CYCLUS
Loading...
Searching...
No Matches
error.h
Go to the documentation of this file.
1#ifndef CYCLUS_SRC_ERROR_H_
2#define CYCLUS_SRC_ERROR_H_
3
4#include <exception>
5#include <iostream>
6#include <map>
7#include <string>
8
9namespace cyclus {
10
11/// A generic mechanism to manually manage exceptions
12class Error : public std::exception {
13 public:
14 /// Constructs a new Error with the default message.
16
17 /// Constructs a new Error with a provided message
18 Error(std::string msg);
19
20 /// Returns the error message associated with this Error.
21 virtual const char* what() const throw();
22
23 /// Returns the error message associated with this Error.
24 std::string msg() const {
25 return msg_;
26 }
27
28 /// sets the error message
29 void msg(std::string msg) {
30 msg_ = msg;
31 }
32
33 virtual ~Error() throw() {}
34
35 protected:
36 /// The message associated with this exception.
37 std::string msg_;
38};
39
40/// For values that are too big, too small, etc.
41class ValueError : public Error {
42 public:
43 ValueError(std::string msg) : Error(msg) {}
44};
45
46/// For failed retrieval/insertion of key-based data into/from data structures
47class KeyError : public Error {
48 public:
49 KeyError(std::string msg) : Error(msg) {}
50};
51
52/// For failed object state expectations
53class StateError : public Error {
54 public:
55 StateError(std::string msg) : Error(msg) {}
56};
57
58/// for failed reading/writing to files, network connections, etc..
59class IOError : public Error {
60 public:
61 IOError(std::string msg) : Error(msg) {}
62};
63
64/// For failed casts that shouldn't
65class CastError : public Error {
66 public:
67 CastError(std::string msg) : Error(msg) {}
68};
69
70/// For validating files received via IO.
71class ValidationError : public Error {
72 public:
73 ValidationError(std::string msg) : Error(msg) {}
74};
75
76/// For depricating API until a next major release
77class DepricationError : public Error {
78 public:
79 DepricationError(std::string msg) : Error(msg) {}
80};
81
82
95
96
97/// This is maximum number of times to issue a warning of each kind.
98extern unsigned int warn_limit;
99
100/// Flag for throwing an error when a warning happens
101extern bool warn_as_error;
102
103/// The number of warnings issues for each kind.
104extern std::map<Warnings, unsigned int> warn_count;
105
106/// Creates the warning prefixes mapping.
107std::map<Warnings, std::string> warn_prefixes();
108
109/// The number of warnings issues for each kind.
110extern std::map<Warnings, std::string> warn_prefix;
111
112/// Issue a warning with the approriate message, accoring to the current
113/// warning settings.
114template <Warnings T>
115void Warn(const std::string& msg) {
116 if (warn_as_error) {
117 switch (T) {
118 case VALUE_WARNING:
119 throw ValueError(msg);
120 case KEY_WARNING:
121 throw KeyError(msg);
122 case STATE_WARNING:
123 throw StateError(msg);
124 case IO_WARNING:
125 throw IOError(msg);
126 case CAST_WARNING:
127 throw CastError(msg);
129 throw ValidationError(msg);
130 default:
131 throw Error(msg);
132 }
133 }
134 unsigned int cnt = warn_count[T]++;
135 if (cnt < warn_limit) {
136 std::cerr << warn_prefix[T] << ": " << msg << "\n";
137 } else if (cnt == 0) {
138 } else if (cnt == warn_limit) {
139 std::cerr << "Further " << warn_prefix[T] << "s will be suppressed.\n";
140 }
141}
142
143} // namespace cyclus
144
145#endif // CYCLUS_SRC_ERROR_H_
For failed casts that shouldn't.
Definition error.h:65
CastError(std::string msg)
Definition error.h:67
For depricating API until a next major release.
Definition error.h:77
DepricationError(std::string msg)
Definition error.h:79
A generic mechanism to manually manage exceptions.
Definition error.h:12
void msg(std::string msg)
sets the error message
Definition error.h:29
virtual const char * what() const
Returns the error message associated with this Error.
Definition error.cc:9
virtual ~Error()
Definition error.h:33
std::string msg() const
Returns the error message associated with this Error.
Definition error.h:24
Error()
Constructs a new Error with the default message.
std::string msg_
The message associated with this exception.
Definition error.h:37
for failed reading/writing to files, network connections, etc..
Definition error.h:59
IOError(std::string msg)
Definition error.h:61
For failed retrieval/insertion of key-based data into/from data structures.
Definition error.h:47
KeyError(std::string msg)
Definition error.h:49
For failed object state expectations.
Definition error.h:53
StateError(std::string msg)
Definition error.h:55
For validating files received via IO.
Definition error.h:71
ValidationError(std::string msg)
Definition error.h:73
For values that are too big, too small, etc.
Definition error.h:41
ValueError(std::string msg)
Definition error.h:43
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
bool warn_as_error
Flag for throwing an error when a warning happens.
Definition error.cc:15
unsigned int warn_limit
This is maximum number of times to issue a warning of each kind.
Definition error.cc:13
std::map< Warnings, std::string > warn_prefix
The number of warnings issues for each kind.
Definition error.cc:34
std::map< Warnings, std::string > warn_prefixes()
Creates the warning prefixes mapping.
Definition error.cc:20
std::map< Warnings, unsigned int > warn_count
The number of warnings issues for each kind.
Definition error.cc:17
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters
void Warn(const std::string &msg)
Issue a warning with the approriate message, accoring to the current warning settings.
Definition error.h:115
Warnings
Definition error.h:83
@ KEY_WARNING
Definition error.h:86
@ VALIDATION_WARNING
Definition error.h:90
@ PENDING_DEPRECATION_WARNING
Definition error.h:92
@ STATE_WARNING
Definition error.h:87
@ IO_WARNING
Definition error.h:88
@ WARNING
Definition error.h:84
@ CAST_WARNING
Definition error.h:89
@ EXPERIMENTAL_WARNING
Definition error.h:93
@ DEPRECATION_WARNING
Definition error.h:91
@ VALUE_WARNING
Definition error.h:85