PDI 1.7.1

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 <sstream>
31#include <string>
32
33#include <paraconf.h>
34
35#include <spdlog/spdlog.h>
36
37#include <pdi/pdi_fwd.h>
38
39namespace PDI {
40
41class PDI_EXPORT Error: public std::exception
42{
43protected:
46
48 std::string m_what;
49
50public:
55
62 template <typename S, typename... Args>
63 Error(PDI_status_t errcode, const S& format_str, Args&& ... args)
64 : m_status{errcode}
65 , m_what{fmt::format(format_str, std::forward<Args>(args)...)}
66 {}
67
72 Error(PDI_status_t errcode, const char* message);
73
74 const char* what() const noexcept override;
75
79 PDI_status_t status() const noexcept;
80};
81
82class PDI_EXPORT Unavailable_error: public Error
83{
84public:
85 template <typename S, typename... Args>
86 Unavailable_error(const S& format_str, Args&& ... args)
87 : Error(PDI_UNAVAILABLE, std::string("Unavailable_error: ") + format_str, std::forward<Args>(args)...)
88 {}
89
91
93};
94
95class PDI_EXPORT Config_error: public Error
96{
97public:
98 template <typename S, typename... Args>
99 Config_error(PC_tree_t tree, const S& format_str, Args&& ... args)
101 {
102 std::ostringstream err_msg;
103 if (!PC_status(tree) && tree.node) {
104 if (tree.node->start_mark.line == tree.node->end_mark.line) {
105 err_msg << "Config_error in line " << tree.node->start_mark.line + 1 << ": ";
106 } else {
107 err_msg << "Config_error in lines " << tree.node->start_mark.line + 1 << " - " << tree.node->end_mark.line << ": ";
108 }
109 } else {
110 err_msg << "Config_error: ";
111 }
112 err_msg << fmt::format(format_str, std::forward<Args>(args)...);
113 m_what = err_msg.str();
114 }
115
117
118 Config_error(const Config_error&) = default;
119};
120
121class PDI_EXPORT Value_error: public Error
122{
123public:
124 template <typename S, typename... Args>
125 Value_error(const S& format_str, Args&& ... args)
126 : Error(PDI_ERR_VALUE, std::string("Value_error: ") + format_str, std::forward<Args>(args)...)
127 {}
128
130
131 Value_error(const Value_error&) = default;
132};
133
134class PDI_EXPORT Plugin_error: public Error
135{
136public:
137 template <typename S, typename... Args>
138 Plugin_error(const S& format_str, Args&& ... args)
139 : Error(PDI_ERR_PLUGIN, std::string("Plugin_error: ") + format_str, std::forward<Args>(args)...)
140 {}
141
143
144 Plugin_error(const Plugin_error&) = default;
145};
146
147class PDI_EXPORT Impl_error: public Error
148{
149public:
150 template <typename S, typename... Args>
151 Impl_error(const S& format_str, Args&& ... args)
152 : Error(PDI_ERR_IMPL, std::string("Impl_error: ") + format_str, std::forward<Args>(args)...)
153 {}
154
155 Impl_error(Impl_error&&) = default;
156
157 Impl_error(const Impl_error&) = default;
158};
159
160class PDI_EXPORT System_error: public Error
161{
162public:
163 template <typename S, typename... Args>
164 System_error(const S& format_str, Args&& ... args)
165 : Error(PDI_ERR_SYSTEM, std::string("System_error: ") + format_str, std::forward<Args>(args)...)
166 {}
167
169
170 System_error(const System_error&) = default;
171};
172
173class PDI_EXPORT State_error: public Error
174{
175public:
176 template <typename S, typename... Args>
177 State_error(const S& format_str, Args&& ... args)
178 : Error(PDI_ERR_STATE, std::string("State_error: ") + format_str, std::forward<Args>(args)...)
179 {}
180
182
183 State_error(const State_error&) = default;
184};
185
186class PDI_EXPORT Right_error: public Error
187{
188public:
189 template <typename S, typename... Args>
190 Right_error(const S& format_str, Args&& ... args)
191 : Error(PDI_ERR_RIGHT, std::string("Right_error: ") + format_str, std::forward<Args>(args)...)
192 {}
193
195
196 Right_error(const Right_error&) = default;
197};
198
199class PDI_EXPORT Type_error: public Error
200{
201public:
202 template <typename S, typename... Args>
203 Type_error(const S& format_str, Args&& ... args)
204 : Error(PDI_ERR_TYPE, std::string("Type_error: ") + format_str, std::forward<Args>(args)...)
205 {}
206
207 Type_error(Type_error&&) = default;
208
209 Type_error(const Type_error&) = default;
210};
211
212} // namespace PDI
213
214#endif // PDI_ERROR_H_
Definition: error.h:96
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:99
Definition: error.h:42
Error(PDI_status_t errcode)
Creates a PDI error without a message.
PDI_status_t m_status
status of the error
Definition: error.h:45
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:63
const char * what() const noexcept override
std::string m_what
message of the error
Definition: error.h:48
Definition: error.h:148
Impl_error(const Impl_error &)=default
Impl_error(Impl_error &&)=default
Impl_error(const S &format_str, Args &&... args)
Definition: error.h:151
Definition: error.h:135
Plugin_error(const S &format_str, Args &&... args)
Definition: error.h:138
Plugin_error(Plugin_error &&)=default
Plugin_error(const Plugin_error &)=default
Definition: error.h:187
Right_error(Right_error &&)=default
Right_error(const Right_error &)=default
Right_error(const S &format_str, Args &&... args)
Definition: error.h:190
Definition: error.h:174
State_error(State_error &&)=default
State_error(const S &format_str, Args &&... args)
Definition: error.h:177
State_error(const State_error &)=default
Definition: error.h:161
System_error(const S &format_str, Args &&... args)
Definition: error.h:164
System_error(const System_error &)=default
System_error(System_error &&)=default
Definition: error.h:200
Type_error(Type_error &&)=default
Type_error(const Type_error &)=default
Type_error(const S &format_str, Args &&... args)
Definition: error.h:203
Definition: error.h:83
Unavailable_error(Unavailable_error &&)=default
Unavailable_error(const Unavailable_error &)=default
Unavailable_error(const S &format_str, Args &&... args)
Definition: error.h:86
Definition: error.h:122
Value_error(Value_error &&)=default
Value_error(const Value_error &)=default
Value_error(const S &format_str, Args &&... args)
Definition: error.h:125
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.