PDI 1.5.5

the PDI data interface

error.h
1/*******************************************************************************
2 * Copyright (C) 2015-2020 Commissariat a l'energie atomique et aux energies alternatives (CEA)
3 * Copyright (C) 2021 Institute of Bioorganic Chemistry Polish Academy of Science (PSNC)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of CEA nor the names of its contributors may be used to
14 * endorse or promote products derived from this software without specific
15 * prior written permission.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 ******************************************************************************/
25
26#ifndef PDI_ERROR_H_
27#define PDI_ERROR_H_
28
29#include <exception>
30#include <string>
31#include <sstream>
32
33#include <paraconf.h>
34
35#include <fmt/core.h>
36
37#include <pdi/pdi_fwd.h>
38
39namespace PDI {
40
41class PDI_EXPORT Error:
42 public std::exception
43{
44protected:
47
49 std::string m_what;
50
51public:
56
63 template<typename S, typename... Args>
64 Error(PDI_status_t errcode, const S& format_str, Args&& ... args):
65 m_status{errcode},
66 m_what{fmt::format(format_str, std::forward<Args>(args)...)}
67 {}
68
73 Error(PDI_status_t errcode, const char* message);
74
75 const char* what() const noexcept override;
76
80 PDI_status_t status() const noexcept;
81
82};
83
84class PDI_EXPORT Unavailable_error:
85 public Error
86{
87public:
88 template<typename S, typename... Args>
89 Unavailable_error(const S& format_str, Args&& ... args):
90 Error(PDI_UNAVAILABLE, std::string("Unavailable_error: ") + format_str, std::forward<Args>(args)...)
91 {}
92
94
96};
97
98class PDI_EXPORT Config_error:
99 public Error
100{
101public:
102 template<typename S, typename... Args>
103 Config_error(PC_tree_t tree, const S& format_str, Args&& ... args):
105 {
106 std::ostringstream err_msg;
107 if (!PC_status(tree) && tree.node) {
108 if (tree.node->start_mark.line == tree.node->end_mark.line) {
109 err_msg << "Config_error in line " << tree.node->start_mark.line + 1 << ": ";
110 } else {
111 err_msg << "Config_error in lines " << tree.node->start_mark.line + 1 << " - " << tree.node->end_mark.line << ": ";
112 }
113 } else {
114 err_msg << "Config_error: ";
115 }
116 err_msg << fmt::format(format_str, std::forward<Args>(args)...);
117 m_what = err_msg.str();
118 }
119
121
122 Config_error(const Config_error&) = default;
123};
124
125class PDI_EXPORT Value_error:
126 public Error
127{
128public:
129 template<typename S, typename... Args>
130 Value_error(const S& format_str, Args&& ... args):
131 Error(PDI_ERR_VALUE, std::string("Value_error: ") + format_str, std::forward<Args>(args)...)
132 {}
133
135
136 Value_error(const Value_error&) = default;
137};
138
139class PDI_EXPORT Plugin_error:
140 public Error
141{
142public:
143 template<typename S, typename... Args>
144 Plugin_error(const S& format_str, Args&& ... args):
145 Error(PDI_ERR_PLUGIN, std::string("Plugin_error: ") + format_str, std::forward<Args>(args)...)
146 {}
147
149
150 Plugin_error(const Plugin_error&) = default;
151};
152
153class PDI_EXPORT Impl_error:
154 public Error
155{
156public:
157 template<typename S, typename... Args>
158 Impl_error(const S& format_str, Args&& ... args):
159 Error(PDI_ERR_IMPL, std::string("Impl_error: ") + format_str, std::forward<Args>(args)...)
160 {}
161
162 Impl_error(Impl_error&&) = default;
163
164 Impl_error(const Impl_error&) = default;
165};
166
167class PDI_EXPORT System_error:
168 public Error
169{
170public:
171 template<typename S, typename... Args>
172 System_error(const S& format_str, Args&& ... args):
173 Error(PDI_ERR_SYSTEM, std::string("System_error: ") + format_str, std::forward<Args>(args)...)
174 {}
175
177
178 System_error(const System_error&) = default;
179};
180
181class PDI_EXPORT State_error:
182 public Error
183{
184public:
185 template<typename S, typename... Args>
186 State_error(const S& format_str, Args&& ... args):
187 Error(PDI_ERR_STATE, std::string("State_error: ") + format_str, std::forward<Args>(args)...)
188 {}
189
191
192 State_error(const State_error&) = default;
193};
194
195class PDI_EXPORT Right_error:
196 public Error
197{
198public:
199 template<typename S, typename... Args>
200 Right_error(const S& format_str, Args&& ... args):
201 Error(PDI_ERR_RIGHT, std::string("Right_error: ") + format_str, std::forward<Args>(args)...)
202 {}
203
205
206 Right_error(const Right_error&) = default;
207};
208
209class PDI_EXPORT Type_error:
210 public Error
211{
212public:
213 template<typename S, typename... Args>
214 Type_error(const S& format_str, Args&& ... args):
215 Error(PDI_ERR_TYPE, std::string("Type_error: ") + format_str, std::forward<Args>(args)...)
216 {}
217
218 Type_error(Type_error&&) = default;
219
220 Type_error(const Type_error&) = default;
221};
222
223} // namespace PDI
224
225#endif // PDI_ERROR_H_
Definition: error.h:100
Config_error(const Config_error &)=default
Config_error(Config_error &&)=default
Config_error(PC_tree_t tree, const S &format_str, Args &&... args)
Definition: error.h:103
Definition: error.h:43
Error(PDI_status_t errcode)
Creates a PDI error without a message.
PDI_status_t m_status
status of the error
Definition: error.h:46
Error(PDI_status_t errcode, const char *message)
Creates a PDI error.
Error(PDI_status_t errcode, const S &format_str, Args &&... args)
Creates a PDI error.
Definition: error.h:64
const char * what() const noexcept override
std::string m_what
message of the error
Definition: error.h:49
Definition: error.h:155
Impl_error(const Impl_error &)=default
Impl_error(Impl_error &&)=default
Impl_error(const S &format_str, Args &&... args)
Definition: error.h:158
Definition: error.h:141
Plugin_error(const S &format_str, Args &&... args)
Definition: error.h:144
Plugin_error(Plugin_error &&)=default
Plugin_error(const Plugin_error &)=default
Definition: error.h:197
Right_error(Right_error &&)=default
Right_error(const Right_error &)=default
Right_error(const S &format_str, Args &&... args)
Definition: error.h:200
Definition: error.h:183
State_error(State_error &&)=default
State_error(const S &format_str, Args &&... args)
Definition: error.h:186
State_error(const State_error &)=default
Definition: error.h:169
System_error(const S &format_str, Args &&... args)
Definition: error.h:172
System_error(const System_error &)=default
System_error(System_error &&)=default
Definition: error.h:211
Type_error(Type_error &&)=default
Type_error(const Type_error &)=default
Type_error(const S &format_str, Args &&... args)
Definition: error.h:214
Definition: error.h:86
Unavailable_error(Unavailable_error &&)=default
Unavailable_error(const Unavailable_error &)=default
Unavailable_error(const S &format_str, Args &&... args)
Definition: error.h:89
Definition: error.h:127
Value_error(Value_error &&)=default
Value_error(const Value_error &)=default
Value_error(const S &format_str, Args &&... args)
Definition: error.h:130
PDI_status_t
Error codes of PDI.
Definition: pdi.h:73
@ PDI_UNAVAILABLE
on an input call, no such data is available
Definition: pdi.h:77
@ PDI_ERR_VALUE
A value expression is invalid.
Definition: pdi.h:81
@ PDI_ERR_STATE
A call to a function has been made at a wrong time (e.g.
Definition: pdi.h:91
@ PDI_ERR_CONFIG
The configuration file is invalid.
Definition: pdi.h:79
@ PDI_ERR_TYPE
Invalid type error.
Definition: pdi.h:95
@ PDI_ERR_SYSTEM
A system error occured (OS, etc.)
Definition: pdi.h:87
@ PDI_ERR_IMPL
Implementation limitation (typically an unimplemented feature)
Definition: pdi.h:85
@ PDI_ERR_PLUGIN
Tried to load a non-existing plugin.
Definition: pdi.h:83
@ PDI_ERR_RIGHT
A conflict of onwership over a content has been raised.
Definition: pdi.h:93
Definition: array_datatype.h:38
STL namespace.