version  0.0.1
Defines the C++ API for MsPASS
Loading...
Searching...
No Matches
MsPASSError.h
1#ifndef _MSPASS_ERROR_H_
2#define _MSPASS_ERROR_H_
3#include <exception>
4#include <iostream>
5namespace mspass {
6namespace utility {
7
14enum class ErrorSeverity {
15 Fatal,
16 Invalid,
17 Suspect,
18 Complaint,
19 Debug,
20 Informational
21};
22
25mspass::utility::ErrorSeverity string2severity(const std::string howbad);
27std::string severity2string(const mspass::utility::ErrorSeverity es);
28
38class MsPASSError : public std::exception {
39public:
44 message = "MsPASS library error\n";
45 badness = ErrorSeverity::Fatal;
46 };
58 MsPASSError(const std::string mess, const char *howbad) {
59 message = mess;
60 std::string s(howbad);
61 /* this small helper function parses s to conver to the
62 enum class of badness*/
63 badness = string2severity(s);
64 };
73 MsPASSError(const std::string mess,
74 const ErrorSeverity s = ErrorSeverity::Invalid) {
75 message = mess;
76 badness = s;
77 };
80 MsPASSError(const char *mess, const ErrorSeverity s) {
81 message = std::string(mess);
82 badness = s;
83 };
87 void log_error() { std::cerr << message << std::endl; };
89 void log_error(std::ostream &ofs) { ofs << message << std::endl; }
100 const char *what() const noexcept { return message.c_str(); };
102 ErrorSeverity severity() const { return badness; };
103
104protected:
108 std::string message;
110 ErrorSeverity badness;
111};
112/* Helper function prototypes. The following set of functions proved necessary
113because of a limitation in pybind11. It appears there is no easy way to bind
114a full interface to MsPASSError when it is handled as an exception. As a
115result neither the badness attribute (if made public) nor the severity method
116are visible when treated as an exception in python. To circumvent this the
117what method writes a string representation of badness to the message. Bound
118versions of the functions below can be used in python programs to test severity
119of a thrown MsPASSError.
120*/
126bool error_says_data_bad(const mspass::utility::MsPASSError &err);
127
137std::string
138parse_message_error_severity(const mspass::utility::MsPASSError &err);
148mspass::utility::ErrorSeverity
149message_error_severity(const mspass::utility::MsPASSError &err);
150} // namespace utility
151} // namespace mspass
152#endif
Base class for error object thrown by MsPASS library routines.
Definition MsPASSError.h:38
MsPASSError(const std::string mess, const ErrorSeverity s=ErrorSeverity::Invalid)
Definition MsPASSError.h:73
MsPASSError()
Definition MsPASSError.h:43
const char * what() const noexcept
Definition MsPASSError.h:100
ErrorSeverity severity() const
Definition MsPASSError.h:102
std::string message
Definition MsPASSError.h:108
MsPASSError(const std::string mess, const char *howbad)
Construct from a std::string with badness defined by keywords in a string.
Definition MsPASSError.h:58
void log_error(std::ostream &ofs)
Definition MsPASSError.h:89
void log_error()
Definition MsPASSError.h:87
MsPASSError(const char *mess, const ErrorSeverity s)
Definition MsPASSError.h:80
ErrorSeverity badness
Definition MsPASSError.h:110