-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexception.hpp
147 lines (116 loc) · 3.95 KB
/
exception.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* \file
* Exceptions and error codes.
*
* \copyright
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#ifndef COSIM_EXCEPTION_HPP
#define COSIM_EXCEPTION_HPP
#include <system_error>
namespace cosim
{
/// Error conditions specific to this library.
enum class errc
{
success = 0,
/// An input file is corrupted or invalid.
bad_file,
/// The requested feature (e.g. an FMI feature) is unsupported.
unsupported_feature,
/// Error loading dynamic library (e.g. model code)
dl_load_error,
/// The model reported an error
model_error,
/**
* One or more variable values are out of range or otherwise invalid,
* but the simulation can proceed anyway.
*
* Since this error condition is usually acceptable, and therefore needs
* to be handled separately from other simulation errors, it has its own
* exception class: `cosim::nonfatal_bad_value`
*/
nonfatal_bad_value,
/// Simulation error
simulation_error,
/// Invalid system structure (e.g. an invalid variable connection)
invalid_system_structure,
/// ZIP file error
zip_error
};
/// A category for errors specific to this library.
const std::error_category& error_category() noexcept;
/// Constructs a library-specific error condition.
std::error_condition make_error_condition(errc e) noexcept;
/// Constructs an error code for a library-specific error condition.
std::error_code make_error_code(errc e) noexcept;
/**
* The base class for exceptions specific to this library.
*
* Most exceptions thrown by functions in this library will be of this type,
* and some may be of a subclass type if they need to carry extra information.
*
* The `code()` function returns an `std::error_code` that specifies more
* precisely which error occurred. Usually, this code will correspond to one
* of the error conditions defined in `cosim::errc`, but this is not always the
* case. (For example, it may also correspond to one of the `std::errc`
* conditions.)
*/
class error : public std::runtime_error
{
public:
/// Constructs an exception with the given error code.
explicit error(std::error_code ec)
: std::runtime_error(ec.message())
, code_(ec)
{ }
/**
* Constructs an exception with the given error code and an additional
* error message.
*
* The `what()` function is guaranteed to return a string which contains
* the text in `msg` in addition to the standard message associated with
* `ec`.
*/
error(std::error_code ec, const std::string& msg)
: std::runtime_error(ec.message() + ": " + msg)
, code_(ec)
{ }
/// Returns an error code.
const std::error_code& code() const noexcept { return code_; }
private:
std::error_code code_;
};
/**
* An exception which indicates that one or more variable values are out
* of range or otherwise invalid, but the simulation can proceed anyway.
*
* This is merely a `cosim::error` with code `errc::nonfatal_bad_value`.
* Since this error condition is usually acceptable, and therefore needs
* to be handled separately from other simulation errors, it has its own
* exception class.
*/
class nonfatal_bad_value : public error
{
public:
/// Constructs an exception with a default error message.
nonfatal_bad_value()
: error(make_error_code(errc::nonfatal_bad_value))
{ }
/// Constructs an exception with a custom error message.
explicit nonfatal_bad_value(const std::string& msg)
: error(make_error_code(errc::nonfatal_bad_value), msg)
{ }
};
} // namespace cosim
namespace std
{
// Enable implicit conversions from errc to std::error_condition.
template<>
struct is_error_condition_enum<cosim::errc> : public true_type
{
};
} // namespace std
#endif // header guard