GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GException.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GException.cpp - Exception handler *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2006-2021 by Juergen Knoedlseder *
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  * @file GException.cpp
23  * @brief Exception handler interface implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #include "GException.hpp"
29 #include "GTools.hpp"
30 #include "GFitsCfitsio.hpp"
31 #include "GEnergy.hpp"
32 
33 
34 /***********************************************************************//**
35  * @brief Exception message.
36  ***************************************************************************/
37 const char* GExceptionHandler::what() const throw()
38 {
39  // Set error message
40  std::string message = "*** ERROR in " + m_origin + ": " + m_message;
41 
42  // Return message as C character array
43  return (gammalib::tochar(message));
44 }
45 
46 
47 /***********************************************************************//**
48  * @brief Invalid value
49  *
50  * @param[in] origin Name of method that has thrown the exception.
51  * @param[in] message Error message.
52  ***************************************************************************/
53 GException::invalid_value::invalid_value(const std::string& origin,
54  const std::string& message)
55 {
56  // Set origin
57  m_origin = origin;
58 
59  // Set message string
60  m_message = "Invalid value.";
61  if (message.length() > 0) {
62  m_message += (" " + message);
63  }
64 
65  // Return
66  return;
67 }
68 
69 
70 /***********************************************************************//**
71  * @brief Invalid argument
72  *
73  * @param[in] origin Name of method that has thrown the exception.
74  * @param[in] message Error message.
75  ***************************************************************************/
77  const std::string& message)
78 {
79  // Set origin
80  m_origin = origin;
81 
82  // Set message string
83  m_message = "Invalid argument.";
84  if (message.length() > 0) {
85  m_message += (" " + message);
86  }
87 
88  // Return
89  return;
90 }
91 
92 
93 /***********************************************************************//**
94  * @brief Invalid argument
95  *
96  * @param[in] origin Name of method that has thrown the exception.
97  * @param[in] argument Argument name.
98  * @param[in] message Optional message.
99  *
100  * This exception signals that a specified argument was not valid.
101  ***************************************************************************/
103  const std::string& argument,
104  const std::string& message)
105 {
106  // Set origin
107  m_origin = origin;
108 
109  // Set message string
110  m_message = "Invalid argument \""+argument+"\" value.";
111 
112  // Add optional error message
113  if (message.length() > 0) {
114  m_message += (" " + message);
115  }
116 
117  // Return
118  return;
119 }
120 
121 
122 /***********************************************************************//**
123  * @brief Invalid return value
124  *
125  * @param[in] origin Name of method that has thrown the exception.
126  * @param[in] message Error message.
127  ***************************************************************************/
129  const std::string& message)
130 {
131  // Set origin
132  m_origin = origin;
133 
134  // Set message string
135  m_message = "Invalid return value.";
136  if (message.length() > 0) {
137  m_message += (" " + message);
138  }
139 
140  // Return
141  return;
142 }
143 
144 
145 /***********************************************************************//**
146  * @brief Index is out of range [0,elements-1]
147  *
148  * @param[in] origin Method throwing the exception.
149  * @param[in] what Describes what is out of range.
150  * @param[in] index Index.
151  * @param[in] elements Number of elements.
152  * @param[in] message Optional error message.
153  *
154  * The @p what string specifies the index type that is out of range. For
155  * example what="Vector index" will lead to "Vector index <index> is ...".
156  * The first letter in @p what is expected to be a capital letter.
157  ***************************************************************************/
158 GException::out_of_range::out_of_range(const std::string& origin,
159  const std::string& what,
160  const int& index,
161  const int& elements,
162  const std::string& message)
163 {
164  // Set origin
165  m_origin = origin;
166 
167  // Set message
168  if (elements > 0) {
169  m_message = gammalib::strip_whitespace(what) + " " +
170  gammalib::str(index) + " is outside the"
171  " valid range [0," + gammalib::str(elements-1) + "].";
172  }
173  else {
174  m_message = "Invalid access to empty object with " +
176  " " + gammalib::str(index) + ".";
177  }
178  if (message.length() > 0) {
179  m_message += (" " + message);
180  }
181 
182  // Return
183  return;
184 }
185 
186 
187 /***********************************************************************//**
188  * @brief Runtime error
189  *
190  * @param[in] origin Name of method that has thrown the exception.
191  * @param[in] message Error message.
192  ***************************************************************************/
194  const std::string& message)
195 {
196  // Set origin
197  m_origin = origin;
198 
199  // Set message string
200  m_message = "Runtime error.";
201  if (message.length() > 0) {
202  m_message += (" " + message);
203  }
204 
205  // Return
206  return;
207 }
208 
209 
210 /***********************************************************************//**
211  * @brief General FITS error
212  *
213  * @param[in] origin Method that throws the error.
214  * @param[in] status cfitsio status.
215  * @param[in] message Optional error message.
216  ***************************************************************************/
217 GException::fits_error::fits_error(const std::string& origin,
218  const int& status,
219  const std::string& message)
220 {
221  // Set origin
222  m_origin = origin;
223 
224  // Set FITS message
225  char err_text[31];
226  __ffgerr(status, err_text);
227  m_message = std::string(err_text);
228  m_message += " (status=" + gammalib::str(status) + ").";
229 
230  // Add optional error message
231  if (message.length() > 0) {
232  m_message += (" " + message);
233  }
234 
235  // Return
236  return;
237 }
238 
239 
240 /***********************************************************************//**
241  * @brief File error
242  *
243  * @param[in] origin Name of method that has thrown the exception.
244  * @param[in] message Error message.
245  ***************************************************************************/
246 GException::file_error::file_error(const std::string& origin,
247  const std::string& message)
248 {
249  // Set origin
250  m_origin = origin;
251 
252  // Set message string
253  m_message = "File error.";
254  if (message.length() > 0) {
255  m_message += (" " + message);
256  }
257 
258  // Return
259  return;
260 }
261 
262 
263 /***********************************************************************//**
264  * @brief Feature not implement
265  *
266  * @param[in] origin Name of method that has thrown the exception.
267  * @param[in] message Optional message.
268  *
269  * This exception signals features that are not yet implemented. It may be
270  * thrown by modules that are still under development.
271  ***************************************************************************/
273  const std::string& message)
274 {
275  // Set origin
276  m_origin = origin;
277 
278  // Set message string
279  if (message.length() > 0) {
280  m_message = message;
281  }
282  else {
283  m_message = "Feature not implemented.";
284  }
285  m_message += " In case that you need this feature for your application"
286  " please submit a feature request on"
287  " https://cta-redmine.irap.omp.eu/projects/gammalib/,"
288  " join this error message and provide a detailed"
289  " description of your needs.";
290 
291  // Return
292  return;
293 }
294 
295 
296 /***********************************************************************//**
297  * @brief Test nested try error
298  *
299  * @param[in] origin Method throwing the exception.
300  * @param[in] message Optional error message.
301  ***************************************************************************/
303  const std::string& message)
304 {
305  // Set origin and message
306  m_origin = origin;
307  m_message = "Nested try error ("+message+")";
308 
309  // Return
310  return;
311 }
312 
313 /***********************************************************************//**
314  * @brief Failure test
315  *
316  * @param[in] origin Method throwing the exception.
317  * @param[in] message Optional error message.
318  *
319  * Used in unit tests to notice a fail in a try block
320  ***************************************************************************/
321 GException::test_failure::test_failure(const std::string& origin,
322  const std::string& message)
323 {
324  // Set origin and message
325  m_origin = origin;
326  m_message = "Failure Test ("+message+")";
327 
328  // Return
329  return;
330 }
331 
332 /***********************************************************************//**
333  * @brief Failure test
334  *
335  * @param[in] origin Method throwing the exception.
336  * @param[in] message Optional error message.
337  *
338  * Used in unit tests to notice a fail in a try block
339  ***************************************************************************/
340 GException::test_error::test_error(const std::string& origin,
341  const std::string& message)
342 {
343  // Set origin and message
344  m_origin = origin;
345  m_message = "Error Test ("+message+")";
346 
347  // Return
348  return;
349 }
350 
351 
352 /***********************************************************************//**
353  * @brief Checks energy interval
354  *
355  * @param[in] origin Method performing the check.
356  * @param[in] emin Minimum energy.
357  * @param[in] emax Maximum energy.
358  *
359  * @exception GException::invalid_argument
360  * Minimum energy is equal or larger than maximum energy.
361  *
362  * Checks that the minimum energy is smaller than the maximum energy.
363  ***************************************************************************/
364 void gammalib::check_energy_interval(const std::string& origin,
365  const GEnergy& emin,
366  const GEnergy& emax)
367 {
368  // Throw an exception if energy interval is invalid
369  if (emin == emax) {
370  std::string msg = "Minimum energy "+emin.print()+" is equal to "
371  "maximum energy "+emax.print()+". Please specify "
372  "a minimum energy that is smaller than the maximum "
373  "energy.";
374  throw GException::invalid_argument(origin, msg);
375  }
376  else if (emin > emax) {
377  std::string msg = "Minimum energy "+emin.print()+" is larger than "
378  "maximum energy "+emax.print()+". Please specify a "
379  "minimum energy that is smaller than the maximum "
380  "energy.";
381  throw GException::invalid_argument(origin, msg);
382  }
383 
384  // Return
385  return;
386 }
387 
388 
389 /***********************************************************************//**
390  * @brief Checks status of GWcs::prj_x2s method
391  *
392  * @param[in] origin Method performing the check.
393  * @param[in] status Status flag.
394  * @param[in] number Number of invalid coordinates.
395  *
396  * @exception GException::invalid_argument
397  * Invalid (x,y) coordinate specified.
398  *
399  * Checks the status of the GWcs::prj_x2s method.
400  ***************************************************************************/
401 void gammalib::check_prj_x2s_status(const std::string& origin,
402  const int& status,
403  const int& number)
404 {
405  // Throw an exception if energy interval is invalid
406  if (status != 0) {
407  if (number == 1) {
408  std::string msg = "One invalid (x,y) coordinate specified. Please "
409  "make sure that only valid (x,y) coordinates "
410  "are specified.";
411  throw GException::invalid_argument(origin, msg);
412  }
413  else {
414  std::string msg = gammalib::str(number)+" invalid (x,y) coordinates "
415  "specified. Please make sure that only valid "
416  "(x,y) coordinates are specified.";
417  throw GException::invalid_argument(origin, msg);
418  }
419  }
420 
421  // Return
422  return;
423 }
424 
425 
426 /***********************************************************************//**
427  * @brief Checks status of GWcs::prj_s2x method
428  *
429  * @param[in] origin Method performing the check.
430  * @param[in] status Status flag.
431  * @param[in] number Number of invalid coordinates.
432  *
433  * @exception GException::invalid_argument
434  * Invalid (phi,theta) coordinate specified.
435  *
436  * Checks the status of the GWcs::prj_s2x method.
437  ***************************************************************************/
438 void gammalib::check_prj_s2x_status(const std::string& origin,
439  const int& status,
440  const int& number)
441 {
442  // Throw an exception if energy interval is invalid
443  if (status != 0) {
444  if (number == 1) {
445  std::string msg = "One invalid (phi,theta) coordinate specified. "
446  "Please make sure that only valid (phi,theta) "
447  "coordinates are specified.";
448  throw GException::invalid_argument(origin, msg);
449  }
450  else {
451  std::string msg = gammalib::str(number)+" invalid (phi,theta) "
452  "coordinates specified. Please make sure that "
453  "only valid (phi,theta) coordinates are specified.";
454  throw GException::invalid_argument(origin, msg);
455  }
456  }
457 
458  // Return
459  return;
460 }
test_error(const std::string &origin, const std::string &message="")
Failure test.
Definition: GException.cpp:340
std::string number(const std::string &noun, const int &number)
Convert singular noun into number noun.
Definition: GTools.cpp:1167
Energy value class definition.
file_error(const std::string &origin, const std::string &message="")
File error.
Definition: GException.cpp:246
invalid_value(const std::string &origin, const std::string &message)
Invalid value.
Definition: GException.cpp:53
Gammalib tools definition.
test_failure(const std::string &origin, const std::string &message="")
Failure test.
Definition: GException.cpp:321
std::string strip_whitespace(const std::string &arg)
Strip leading and trailing whitespace from string.
Definition: GTools.cpp:80
#define __ffgerr(A, B)
test_nested_try_error(const std::string &origin, const std::string &message="")
Test nested try error.
Definition: GException.cpp:302
char * tochar(const std::string &arg)
Convert string to C string.
Definition: GTools.cpp:767
std::string print(const GChatter &chatter=NORMAL) const
Print energy.
Definition: GEnergy.cpp:748
CFITSIO interface header.
out_of_range(const std::string &origin, const std::string &what, const int &index, const int &elements, const std::string &message="")
Index is out of range [0,elements-1].
Definition: GException.cpp:158
runtime_error(const std::string &origin, const std::string &message="")
Runtime error.
Definition: GException.cpp:193
void check_energy_interval(const std::string &origin, const GEnergy &emin, const GEnergy &emax)
Checks energy interval.
Definition: GException.cpp:364
std::string m_message
Definition: GException.hpp:63
virtual const char * what() const
Exception message.
Definition: GException.cpp:37
invalid_return_value(const std::string &origin, const std::string &message)
Invalid return value.
Definition: GException.cpp:128
void check_prj_s2x_status(const std::string &origin, const int &status, const int &number)
Checks status of GWcs::prj_s2x method.
Definition: GException.cpp:438
void check_prj_x2s_status(const std::string &origin, const int &status, const int &number)
Checks status of GWcs::prj_x2s method.
Definition: GException.cpp:401
Exception handler interface definition.
std::string tolower(const std::string &s)
Convert string to lower case.
Definition: GTools.cpp:955
std::string m_origin
Definition: GException.hpp:62
invalid_argument(const std::string &origin, const std::string &message)
Invalid argument.
Definition: GException.cpp:76
feature_not_implemented(const std::string &origin, const std::string &message="")
Feature not implement.
Definition: GException.cpp:272
fits_error(const std::string &origin, const int &status, const std::string &message="")
General FITS error.
Definition: GException.cpp:217
Class that handles energies in a unit independent way.
Definition: GEnergy.hpp:48
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:489