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