CppEphem
CEException.h
Go to the documentation of this file.
1 /***************************************************************************
2  * CEException.cpp: CppEphem *
3  * ----------------------------------------------------------------------- *
4  * Copyright © 2019 JCardenzana *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 
22 #ifndef CEException_h
23 #define CEException_h
24 
25 #include <iostream>
26 #include <string>
27 #include <vector>
28 #include <exception>
29 #include <execinfo.h>
30 
31 class CEExceptionHandler : public std::exception {
32 public:
34  // get void*'s for all entries on the stack
35  void *array[100];
36  int size = backtrace(array, 100);
37 
38  // print out all the frames to stderr
39  char** bcktrace = backtrace_symbols(array, size);
40 
41  backtrace_ += "Exception encountered. Printing backtrace...\n";
42  for (int i = 0; i < size; i++) {
43  backtrace_ += std::string(bcktrace[i]) + "\n";
44  }
45  }
46  CEExceptionHandler(const std::string& origin,
47  const std::string& message,
48  const std::string& type = "<no type>");
49  virtual ~CEExceptionHandler() noexcept {}
50  virtual const char* what() const noexcept;
51  virtual void message_append(const std::string& msg);
52  virtual void message(const std::string& msg);
53  virtual const char* message(void) const noexcept;
54  virtual const char* trace(void) const noexcept;
55 
56 private:
57  std::string origin_;
58  std::string type_;
59  std::string message_;
60  std::string backtrace_;
61  mutable std::string full_msg_;
62 };
63 
64 
65 /***********************************************************************/
70 class CEException : public CEExceptionHandler {
71 public:
72 
73  // When a value is not valid
74  class invalid_value : public CEExceptionHandler {
75  public:
76  invalid_value(const std::string& origin,
77  const std::string& message);
78  };
79 
80  // Invalid delimiter
81  class invalid_delimiter : public CEExceptionHandler {
82  public:
83  invalid_delimiter(const std::string& origin,
84  const std::string& message);
85  };
86 
87  /* ----------------------------------------------------------- *
88  * EXCEPTIONS RELATED TO READING CORRECTIONS FILE
89  * ----------------------------------------------------------- */
91  public:
92  corr_file_load_error(const std::string& origin,
93  const std::string& message);
94  };
95 
96 
97 
98  /* ----------------------------------------------------------- *
99  * EXCEPTIONS RELATED TO SOFA ERRORS
100  * ----------------------------------------------------------- */
101 
102  // When a particular error code is produced by a sofa method
103  class sofa_error : public CEExceptionHandler {
104  public:
105  sofa_error(const std::string& origin,
106  const std::string& sofa_method,
107  const int& errcode,
108  const std::string& message);
109  };
110  // When an exception happens inside a sofa method
111  class sofa_exception : public CEExceptionHandler {
112  public:
113  sofa_exception(const std::string& origin,
114  const std::string& sofa_method,
115  const std::string& message);
116  };
117 
118 };
119 
120 #endif /* CEException_h */
CEExceptionHandler::~CEExceptionHandler
virtual ~CEExceptionHandler() noexcept
Definition: CEException.h:66
CEExceptionHandler::CEExceptionHandler
CEExceptionHandler()
Definition: CEException.h:50
CEExceptionHandler
Definition: CEException.h:30
CEExceptionHandler::full_msg_
std::string full_msg_
Definition: CEException.h:78
CEException::sofa_error
Definition: CEException.h:102
CEExceptionHandler::trace
virtual const char * trace(void) const noexcept
Return the backtrace string.
Definition: CEException.cpp:91
CEExceptionHandler::origin_
std::string origin_
Definition: CEException.h:74
CEExceptionHandler::message_append
virtual void message_append(const std::string &msg)
Append a string to the end of the message.
Definition: CEException.cpp:63
CEException::invalid_delimiter
Definition: CEException.h:80
CEExceptionHandler::message
virtual const char * message(void) const noexcept
Return the message passed by the user.
Definition: CEException.cpp:82
CEException
Definition: CEException.h:69
CEException::sofa_exception
Definition: CEException.h:110
CEExceptionHandler::what
virtual const char * what() const noexcept
Generates and returns the error message.
Definition: CEException.cpp:48
CEExceptionHandler::message_
std::string message_
Definition: CEException.h:76
CEExceptionHandler::backtrace_
std::string backtrace_
Definition: CEException.h:77
CEException::corr_file_load_error
Definition: CEException.h:89
CEExceptionHandler::type_
std::string type_
Definition: CEException.h:75