DryChem 1.0.0
A generic, compile-time C++ toolbox with no dependencies for the modern computational chemistry project.
Loading...
Searching...
No Matches
errorHandling.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-2025 Cody R. Drisko. All rights reserved.
2// Licensed under the MIT License. See the LICENSE file in the project root for more information.
3//
4// Name: errorHandling.hpp
5// Author: cdrisko
6// Date: 01/31/2020-15:38:03
7// Description: Utilities for printing error messages according to an error's severity
8
9#ifndef DRYCHEM_COMMON_UTILITIES_INCLUDE_COMMON_UTILS_ERRORS_UTILS_ERRORHANDLING_HPP
10#define DRYCHEM_COMMON_UTILITIES_INCLUDE_COMMON_UTILS_ERRORS_UTILS_ERRORHANDLING_HPP
11
12#include <cstdlib>
13#include <iostream>
14#include <sstream>
15
18
19namespace CppUtils::Errors
20{
29 template<ErrorSeverity Severity = ErrorSeverity::Warning>
30 inline void printErrorMessage(const ErrorMessage& error)
31 {
32 std::stringstream errorMessage;
33
34 if (error.fileName.empty() && error.lineNumber == 0ul)
35 errorMessage << "\n " << error.message;
36 else
37 errorMessage << ' ' << error.message;
38
39 if constexpr (is_fatal_v<Severity>)
40 {
41 std::cerr << error.programName << " Fatal Error:" << errorMessage.str() << std::endl;
42 std::exit(EXIT_FAILURE);
43 }
44 else
45 std::cerr << error.programName << " Warning:" << errorMessage.str() << std::endl;
46 }
47
50} // namespace CppUtils::Errors
51
52#endif
Definition fatalException.hpp:20
auto printFatalErrorMessage
A convenience function for printing an error message when said error is fatal.
Definition errorHandling.hpp:49
void printErrorMessage(const ErrorMessage &error)
Definition errorHandling.hpp:30
constexpr bool is_fatal_v
Convenience variable template for ease-of-use.
Definition isFatal.hpp:40
Definition errorTypes.hpp:32
std::size_t lineNumber
The line number where the error took place, use the LINE macro.
Definition errorTypes.hpp:36
std::string programName
The name of the program where the exception originated.
Definition errorTypes.hpp:33
std::string fileName
The file name where the error took place, use the FILE macro.
Definition errorTypes.hpp:35
std::string message
The main error message describing the error.
Definition errorTypes.hpp:34