Reactions  0.1.1
Handling reaction trees and decays
exceptions.hpp
Go to the documentation of this file.
1 
5 #pragma once
6 
7 #include <cstring>
8 #include <exception>
9 #include <stdexcept>
10 #include <string>
11 
12 namespace reactions {
13 
15  namespace exceptions::detail {
20  std::string mark_error(std::string const &str, const char *msg,
21  std::size_t rpos) {
22 
23  auto nb = strlen(msg) + 3;
24  auto ps = str.size() - rpos;
25  std::string new_msg;
26  new_msg.reserve(nb + str.size() + ps + 3);
27  new_msg.append(msg);
28  new_msg.append(":\n "); // size 3
29  new_msg.append(str);
30  new_msg.push_back('\n'); // size 1
31  new_msg.append(ps + 1, ' ');
32  new_msg.push_back('^'); // size 1
33  return new_msg;
34  }
35  } // namespace exceptions::detail
36 
39  public:
40  internal_error(const char *msg) : std::runtime_error{msg} {};
41 
42  const char *what() const noexcept { return std::runtime_error::what(); }
43  };
44 
51  public:
52  missing_fields_error(const char *msg) : std::runtime_error{msg} {};
53 
54  const char *what() const noexcept { return std::runtime_error::what(); }
55  };
56 
59  public:
60  syntax_error(const char *msg) : std::runtime_error{msg} {};
61 
62  const char *what() const noexcept { return std::runtime_error::what(); }
63  };
64 
66  namespace exceptions {
67 
70 
71  public:
72  __syntax_error(const char *msg, std::size_t rpos)
73  : m_msg{msg}, m_rpos{rpos} {}
74 
75  const char *what() const noexcept { return m_msg; }
76 
78  return {detail::mark_error(str, m_msg, m_rpos).c_str()};
79  }
80 
81  private:
82  const char *m_msg;
83  std::size_t m_rpos;
84  };
85  } // namespace exceptions
86 
89 
90  public:
91  lookup_error(const char *msg) : std::runtime_error{msg} {};
92 
93  const char *what() const noexcept { return std::runtime_error::what(); }
94  };
95 
102 
103  public:
104  database_error(const char *msg) : std::runtime_error{msg} {};
105 
106  const char *what() const noexcept { return std::runtime_error::what(); }
107  };
108 
111 
112  public:
113  value_error(const char *msg) : std::runtime_error{msg} {};
114 
115  const char *what() const noexcept { return std::runtime_error::what(); }
116  };
117 } // namespace reactions
Syntax error with an unformatted message.
Definition: exceptions.hpp:69
syntax_error update(std::string const &str)
Definition: exceptions.hpp:77
const char * what() const noexcept
Definition: exceptions.hpp:93
Raised when trying to access a field that is not defined.
Definition: exceptions.hpp:50
__syntax_error(const char *msg, std::size_t rpos)
Definition: exceptions.hpp:72
lookup_error(const char *msg)
Definition: exceptions.hpp:91
std::string mark_error(std::string const &str, const char *msg, std::size_t rpos)
Format an error message referring to a syntax error.
Definition: exceptions.hpp:20
STL namespace.
Raised whenever a problem is detected in the database.
Definition: exceptions.hpp:101
Raised when unexpected problems appear, which should be reported as bugs.
Definition: exceptions.hpp:38
const char * what() const noexcept
Definition: exceptions.hpp:106
syntax_error(const char *msg)
Definition: exceptions.hpp:60
database_error(const char *msg)
Definition: exceptions.hpp:104
STL class.
T push_back(T... args)
T what(T... args)
Main namespace of the Reactions package.
Definition: all.hpp:22
T append(T... args)
Raised when an element is not found within a database.
Definition: exceptions.hpp:88
Raised whenever a problem is detected with an input value.
Definition: exceptions.hpp:110
value_error(const char *msg)
Definition: exceptions.hpp:113
missing_fields_error(const char *msg)
Definition: exceptions.hpp:52
T size(T... args)
const char * what() const noexcept
Definition: exceptions.hpp:75
const char * what() const noexcept
Definition: exceptions.hpp:54
Raised when processing the syntax of reactions and decays.
Definition: exceptions.hpp:58
T c_str(T... args)
const char * what() const noexcept
Definition: exceptions.hpp:115
const char * what() const noexcept
Definition: exceptions.hpp:42
internal_error(const char *msg)
Definition: exceptions.hpp:40
T reserve(T... args)
const char * what() const noexcept
Definition: exceptions.hpp:62