Utopia  2
Framework for studying models of complex & adaptive systems.
exceptions.hh
Go to the documentation of this file.
1 #ifndef EXCEPTIONS_HH
2 #define EXCEPTIONS_HH
3 
4 #include <exception>
5 #include <iostream>
6 
7 #include <yaml-cpp/yaml.h>
8 
9 #include "types.hh"
10 
11 
12 namespace Utopia {
13 
15 class Exception : public std::runtime_error {
16 public:
18  const int exit_code;
19 
21 
26  explicit Exception(const std::string& what_arg, const int exit_code_arg=1)
27  :
28  std::runtime_error(what_arg),
29  exit_code(exit_code_arg)
30  {};
31 
33 
38  explicit Exception(const char* what_arg, const int exit_code_arg=1)
39  :
40  std::runtime_error(what_arg),
41  exit_code(exit_code_arg)
42  {};
43 };
44 
45 
47 
51 class GotSignal : public Exception {
52 public:
54 
58  GotSignal(const int signum)
59  :
60  Exception("Received signal: " + std::to_string(signum),
61  128 + abs(signum))
62  {}
63 };
64 
65 
67 class KeyError : public Exception {
68 public:
70  KeyError(const std::string& key,
71  const DataIO::Config& node,
72  const std::string& prefix = "")
73  :
74  Exception(generate_what_arg(key, node, prefix))
75  {}
76 
77 private:
79  std::string generate_what_arg(std::string key,
80  DataIO::Config node,
81  std::string prefix)
82  {
83  std::stringstream msg;
84 
85  if (prefix.length()) {
86  msg << prefix << std::endl;
87  }
88 
89  msg << "KeyError: " << key << std::endl;
90 
91  if (not node) {
92  msg << "The given node is a Zombie! Make sure the node you are "
93  << "trying to read from is valid." << std::endl;
94  }
95  else if (node.size() == 0) {
96  msg << "The given node contains no entries! Make sure the desired "
97  << "key is available."
98  << std::endl;
99  }
100  else {
101  // Emit the node to give more clues at what might have gone wrong
102  msg << "Make sure the desired key is available. The content of "
103  << "the given node is as follows:" << std::endl
104  << YAML::Dump(node) << std::endl;
105  }
106 
107  return msg.str();
108  }
109 };
110 
111 
113 class OutOfSpace : public Exception {
114 public:
116  template<class VecT, class Space>
117  OutOfSpace(const VecT& invalid_pos,
118  const std::shared_ptr<Space>& space,
119  const std::string prefix = {})
120  :
121  Exception([&](){
122  std::stringstream emsg;
123  if (prefix.length() > 0) {
124  emsg << prefix << " ";
125  }
126  emsg << "The given position " << std::endl << invalid_pos
127  << "is not within the non-periodic space with extent"
128  << std::endl << space->extent;
129  return emsg.str();
130  }())
131  {}
132 };
133 
134 
136 
139 template<class exc_t>
140 int handle_exception(exc_t& exc) {
141  std::cerr << exc.what() << std::endl;
142  return exc.exit_code;
143 }
144 
145 } // namespace Utopia
146 
147 #endif // EXCEPTIONS_HH
The base exception class to derive Utopia-specific exceptions from.
Definition: exceptions.hh:15
const int exit_code
The exit code to use when exiting due to this exception.
Definition: exceptions.hh:18
Exception(const char *what_arg, const int exit_code_arg=1)
Construct an Utopia-specific exception.
Definition: exceptions.hh:38
Exception(const std::string &what_arg, const int exit_code_arg=1)
Construct an Utopia-specific exception.
Definition: exceptions.hh:26
An exception for when the program should end due to handling of a signal.
Definition: exceptions.hh:51
GotSignal(const int signum)
Construct a GotSignal exception, which has a standardized what message.
Definition: exceptions.hh:58
For access to a dict-like structure with a bad key.
Definition: exceptions.hh:67
KeyError(const std::string &key, const DataIO::Config &node, const std::string &prefix="")
Construct a KeyError exception, which has a standardized what message.
Definition: exceptions.hh:70
std::string generate_what_arg(std::string key, DataIO::Config node, std::string prefix)
Generates the what argument for the key error.
Definition: exceptions.hh:79
An exception class for invalid positions in Utopia::Space.
Definition: exceptions.hh:113
OutOfSpace(const VecT &invalid_pos, const std::shared_ptr< Space > &space, const std::string prefix={})
Construct the exception with the invalid position and the space given.
Definition: exceptions.hh:117
YAML::Node Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition: types.hh:71
std::string to_string(const Config &node)
Given a config node, returns a string representation of it.
Definition: cfg_utils.hh:110
Definition: agent.hh:11
int handle_exception(exc_t &exc)
A helper function to handle a Utopia-specific exception.
Definition: exceptions.hh:140
Definition: parallel.hh:235