version  0.0.1
Defines the C++ API for MsPASS
MsPASSError.h
1 #ifndef _MSPASS_ERROR_H_
2 #define _MSPASS_ERROR_H_
3 #include <iostream>
4 #include <exception>
5 namespace mspass {
6 namespace utility{
7 
14 enum class ErrorSeverity
15 {
16  Fatal,
17  Invalid,
18  Suspect,
19  Complaint,
20  Debug,
21  Informational
22 };
23 
24 
26 mspass::utility::ErrorSeverity string2severity(const std::string howbad);
28 std::string severity2string(const mspass::utility::ErrorSeverity es);
29 
39 class MsPASSError : public std::exception
40 {
41 public:
42 
47  message="MsPASS library error\n";
48  badness=ErrorSeverity::Fatal;
49  };
60  MsPASSError(const std::string mess,const char *howbad){
61  message=mess;
62  std::string s(howbad);
63  /* this small helper function parses s to conver to the
64  enum class of badness*/
65  badness=string2severity(s);
66  };
75  MsPASSError(const std::string mess,const ErrorSeverity s=ErrorSeverity::Invalid)
76  {
77  message=mess;
78  badness=s;
79  };
82  MsPASSError(const char *mess,const ErrorSeverity s){
83  message=std::string(mess);
84  badness=s;
85  };
89 void log_error(){
90  std::cerr << message << std::endl;
91 };
93 void log_error(std::ostream& ofs)
94 {
95  ofs << message <<std::endl;
96 }
107  const char * what() const noexcept{return message.c_str();};
109  ErrorSeverity severity() const {return badness;};
110 protected:
114  std::string message;
116  ErrorSeverity badness;
117 };
118 /* Helper function prototypes. The following set of functions proved necessary
119 because of a limitation in pybind11. It appears there is no easy way to bind
120 a full interface to MsPASSError when it is handled as an exception. As a result
121 neither the badness attribute (if made public) nor the severity method are
122 visible when treated as an exception in python. To circumvent this the what
123 method writes a string representation of badness to the message. Bound versions
124 of the functions below can be used in python programs to test severity of
125 a thrown MsPASSError.
126 */
132 bool error_says_data_bad(const mspass::utility::MsPASSError& err);
133 
143 std::string parse_message_error_severity(const mspass::utility::MsPASSError& err);
153 mspass::utility::ErrorSeverity message_error_severity(const mspass::utility::MsPASSError& err);
154 } // end utility namespace
155 } // End mspass namespace declaration
156 #endif
Base class for error object thrown by MsPASS library routines.
Definition: MsPASSError.h:40
MsPASSError(const std::string mess, const ErrorSeverity s=ErrorSeverity::Invalid)
Definition: MsPASSError.h:75
MsPASSError()
Definition: MsPASSError.h:46
ErrorSeverity severity() const
Definition: MsPASSError.h:109
std::string message
Definition: MsPASSError.h:109
MsPASSError(const std::string mess, const char *howbad)
Construct from a std::string with badness defined by keywords in a string.
Definition: MsPASSError.h:60
void log_error(std::ostream &ofs)
Definition: MsPASSError.h:93
void log_error()
Definition: MsPASSError.h:89
const char * what() const noexcept
Definition: MsPASSError.h:107
MsPASSError(const char *mess, const ErrorSeverity s)
Definition: MsPASSError.h:82
ErrorSeverity badness
Definition: MsPASSError.h:116