CYCLUS
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 
9 namespace cyclus {
10 
11 /// A generic mechanism to manually manage exceptions
12 class Error : public std::exception {
13  public:
14  /// Constructs a new Error with the default message.
15  Error();
16 
17  /// Constructs a new Error with a provided message
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.
38 };
39 
40 /// For values that are too big, too small, etc.
41 class ValueError : public Error {
42  public:
44 };
45 
46 /// For failed retrieval/insertion of key-based data into/from data structures
47 class KeyError : public Error {
48  public:
50 };
51 
52 /// For failed object state expectations
53 class StateError : public Error {
54  public:
56 };
57 
58 /// for failed reading/writing to files, network connections, etc..
59 class IOError : public Error {
60  public:
62 };
63 
64 /// For failed casts that shouldn't
65 class CastError : public Error {
66  public:
68 };
69 
70 /// For validating files received via IO.
71 class ValidationError : public Error {
72  public:
74 };
75 
76 /// For depricating API until a next major release
77 class DepricationError : public Error {
78  public:
80 };
81 
82 
83 enum Warnings {
84  WARNING = 0,
94 };
95 
96 
97 /// This is maximum number of times to issue a warning of each kind.
98 extern unsigned int warn_limit;
99 
100 /// Flag for throwing an error when a warning happens
101 extern bool warn_as_error;
102 
103 /// The number of warnings issues for each kind.
104 extern std::map<Warnings, unsigned int> warn_count;
105 
106 /// Creates the warning prefixes mapping.
107 std::map<Warnings, std::string> warn_prefixes();
108 
109 /// The number of warnings issues for each kind.
110 extern std::map<Warnings, std::string> warn_prefix;
111 
112 /// Issue a warning with the approriate message, accoring to the current
113 /// warning settings.
114 template <Warnings T>
115 void 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);
128  case VALIDATION_WARNING:
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_
ValueError(std::string msg)
Definition: error.h:43
KeyError(std::string msg)
Definition: error.h:49
A generic mechanism to manually manage exceptions.
Definition: error.h:12
For failed object state expectations.
Definition: error.h:53
CastError(std::string msg)
Definition: error.h:67
std::map< Warnings, unsigned int > warn_count
The number of warnings issues for each kind.
Definition: error.cc:17
For values that are too big, too small, etc.
Definition: error.h:41
For validating files received via IO.
Definition: error.h:71
DepricationError(std::string msg)
Definition: error.h:79
virtual ~Error()
Definition: error.h:33
IOError(std::string msg)
Definition: error.h:61
For failed casts that shouldn&#39;t.
Definition: error.h:65
unsigned int warn_limit
This is maximum number of times to issue a warning of each kind.
Definition: error.cc:13
void msg(std::string msg)
sets the error message
Definition: error.h:29
bool warn_as_error
Flag for throwing an error when a warning happens.
Definition: error.cc:15
std::map< Warnings, std::string > warn_prefixes()
Creates the warning prefixes mapping.
Definition: error.cc:20
void Warn(const std::string &msg)
Issue a warning with the approriate message, accoring to the current warning settings.
Definition: error.h:115
for failed reading/writing to files, network connections, etc..
Definition: error.h:59
Error()
Constructs a new Error with the default message.
std::map< Warnings, std::string > warn_prefix
The number of warnings issues for each kind.
Definition: error.cc:34
For depricating API until a next major release.
Definition: error.h:77
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
std::string msg_
The message associated with this exception.
Definition: error.h:37
virtual const char * what() const
Returns the error message associated with this Error.
Definition: error.cc:9
StateError(std::string msg)
Definition: error.h:55
std::string msg() const
Returns the error message associated with this Error.
Definition: error.h:24
For failed retrieval/insertion of key-based data into/from data structures.
Definition: error.h:47
ValidationError(std::string msg)
Definition: error.h:73
Warnings
Definition: error.h:83