DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
Exception.h
Go to the documentation of this file.
1// This file is part of DynExp.
2
8#pragma once
9
10namespace Util
11{
15 enum class ErrorType { Info, Warning, Error, Fatal };
16
45
50 class Exception : public std::runtime_error
51 {
52 public:
62 Exception(std::string Description = "No exception details available.",
63 const ErrorType Type = ErrorType::Error, const int ErrorCode = -1,
64 const std::source_location Location = std::source_location::current()) noexcept;
65 virtual ~Exception() = default;
66
72 constexpr static const char* GetErrorLabel(const ErrorType Type)
73 {
74 switch (Type)
75 {
76 case ErrorType::Warning: return "Warning";
77 case ErrorType::Error: return "Error";
78 case ErrorType::Fatal: return "Fatal Error";
79 default: return "Info";
80 }
81 }
82
88 constexpr static const char* GetErrorLabelColor(const ErrorType Type)
89 {
90 switch (Type)
91 {
92 case ErrorType::Warning: return "Orange";
93 case ErrorType::Error: return "Red";
94 case ErrorType::Fatal: return "DarkRed";
95 default: return "Blue";
96 }
97 }
98
99 constexpr const char* GetErrorLabel() const { return GetErrorLabel(Type); }
100 constexpr const char* GetErrorLabelColor() const { return GetErrorLabelColor(Type); }
101
102#ifdef DYNEXP_HAS_STACKTRACE
103 auto& GetStackTrace() const { return Trace; }
104#endif // DYNEXP_HAS_STACKTRACE
105
107 const int ErrorCode;
108 const size_t Line;
109 const std::string Function;
110 const std::string File;
111
112 private:
113#ifdef DYNEXP_HAS_STACKTRACE
117 const std::stacktrace Trace;
118#endif // DYNEXP_HAS_STACKTRACE
119 };
120
127 {
128 public:
129 ForwardedException() noexcept : Exception("Unknown forwarded exception") {}
130 ForwardedException(const std::exception& e) noexcept : Exception(e.what()) {}
131 ForwardedException(const Exception& e) noexcept : Exception(e) {}
132 };
133
138 {
139 public:
140 InvalidArgException(std::string Description,
141 const std::source_location Location = std::source_location::current()) noexcept
142 : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::InvalidArg, Location)
143 {}
144 };
145
151 {
152 public:
154 const std::source_location Location = std::source_location::current()) noexcept
155 : Exception(std::move(Description), ErrorType::Error, ErrorCode, Location)
156 {}
157 };
158
164 {
165 public:
166 InvalidDataException(std::string Description,
167 const std::source_location Location = std::source_location::current()) noexcept
168 : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::InvalidData, Location)
169 {}
170 };
171
176 {
177 public:
178 InvalidCallException(std::string Description,
179 const std::source_location Location = std::source_location::current()) noexcept
180 : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::InvalidCall, Location)
181 {}
182 };
183
188 {
189 public:
190 UnderflowException(std::string Description,
191 const std::source_location Location = std::source_location::current()) noexcept
192 : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::Underflow, Location)
193 {}
194 };
195
200 {
201 public:
202 OverflowException(std::string Description,
203 const std::source_location Location = std::source_location::current()) noexcept
204 : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::Overflow, Location)
205 {}
206 };
207
212 {
213 public:
214 OutOfRangeException(std::string Description,
215 const std::source_location Location = std::source_location::current()) noexcept
216 : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::OutOfRange, Location)
217 {}
218 };
219
225 {
226 public:
227 EmptyException(std::string Description,
228 const std::source_location Location = std::source_location::current()) noexcept
229 : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::Empty, Location)
230 {}
231 };
232
237 {
238 public:
239 NotFoundException(std::string Description,
240 const std::source_location Location = std::source_location::current()) noexcept
241 : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::NotFound, Location)
242 {}
243 };
244
249 {
250 public:
251 TypeErrorException(const std::source_location Location = std::source_location::current()) noexcept
252 : Exception("A base pointer did not belong to the expected derived type.",
253 ErrorType::Error, DynExpErrorCodes::TypeError, Location)
254 {}
255 };
256
262 {
263 public:
265 const std::source_location Location = std::source_location::current()) noexcept
266 : Exception(std::move(Description), Type, ErrorCode, Location)
267 {}
268 };
269
275 {
276 public:
277 ThreadDidNotRespondException(const std::source_location Location = std::source_location::current()) noexcept
278 : TimeoutException("Timeout occurred. A thread did not respond. Retry or terminate the program.",
279 ErrorType::Error, DynExpErrorCodes::ThreadDidNotRespond, Location)
280 {}
281 };
282
287 {
288 public:
289 NotAvailableException(std::string Description, const ErrorType Type = ErrorType::Warning,
290 const std::source_location Location = std::source_location::current()) noexcept
291 : Exception(std::move(Description), Type, DynExpErrorCodes::NotAvailable, Location)
292 {}
293 };
294
300 {
301 public:
302 NotImplementedException(const std::source_location Location = std::source_location::current()) noexcept
303 : Exception("This function has not been implemented for the given item type.",
304 ErrorType::Error, DynExpErrorCodes::NotImplemented, Location)
305 {}
306 NotImplementedException(std::string Description,
307 const std::source_location Location = std::source_location::current()) noexcept
308 : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::NotImplemented, Location)
309 {}
310 };
311
316 {
317 public:
318 FileIOErrorException(std::string Filename,
319 const std::source_location Location = std::source_location::current()) noexcept
320 : Exception("Error reading from or writing to file \"" + Filename + "\".",
321 ErrorType::Error, DynExpErrorCodes::FileIOError, Location)
322 {}
323 };
324
330 {
331 public:
332 LinkedObjectNotLockedException(const std::source_location Location = std::source_location::current()) noexcept
334 "A linked object wrapper container is empty. Probably, the respective object link parameter has not been locked calling RunnableInstance::LockObject().",
335 DynExpErrorCodes::LinkedObjectNotLocked, Location)
336 {}
337 };
338
345 {
346 public:
347 InvalidObjectLinkException(const std::source_location Location = std::source_location::current()) noexcept
349 "An object link does not point to a valid destination. Probably, an object referenced by an object link parameter has been removed from the project.",
350 DynExpErrorCodes::InvalidObjectLink, Location)
351 {}
352 };
353
358 {
359 public:
360 ServiceFailedException(std::string Description, const ErrorType Type = ErrorType::Error,
361 const std::source_location Location = std::source_location::current()) noexcept
362 : Exception(std::move(Description), Type, DynExpErrorCodes::ServiceFailed, Location)
363 {}
364 };
365
372 std::ostream& operator<<(std::ostream& stream, const Exception& e);
373
379 void ForwardException(std::exception_ptr e);
380}
Thrown when a list is expected to contain entries and when a query results in an empty answer or an e...
Definition Exception.h:225
EmptyException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:227
DynExp exceptions are derived from this class. It contains basic information about the cause of the e...
Definition Exception.h:51
const std::string File
Source code file where the exception occurred.
Definition Exception.h:110
const size_t Line
Line in source code where the exception occurred.
Definition Exception.h:108
static constexpr const char * GetErrorLabel(const ErrorType Type)
Converts an error type to a user-readable label for logging.
Definition Exception.h:72
constexpr const char * GetErrorLabelColor() const
Definition Exception.h:100
const std::string Function
Function in source code where the exception occurred
Definition Exception.h:109
const ErrorType Type
DynExp error type from Util::ErrorType
Definition Exception.h:106
constexpr const char * GetErrorLabel() const
Definition Exception.h:99
virtual ~Exception()=default
const int ErrorCode
DynExp error code from DynExpErrorCodes::DynExpErrorCodes
Definition Exception.h:107
static constexpr const char * GetErrorLabelColor(const ErrorType Type)
Converts an error type to an HTML color name for logging.
Definition Exception.h:88
Thrown when reading from or writing to a file failed.
Definition Exception.h:316
FileIOErrorException(std::string Filename, const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:318
Class to forward an Exception instance from one DynExp::Object instance to another DynExp::Object ins...
Definition Exception.h:127
ForwardedException() noexcept
Definition Exception.h:129
ForwardedException(const Exception &e) noexcept
Definition Exception.h:131
ForwardedException(const std::exception &e) noexcept
Definition Exception.h:130
An invalid argument like a null pointer has been passed to a function.
Definition Exception.h:138
InvalidArgException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:140
Thrown when a function call is not allowed to a specific thread in a multi-threading context.
Definition Exception.h:176
InvalidCallException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:178
Data to operate on is invalid for a specific purpose. This indicates a corrupted data structure or fu...
Definition Exception.h:164
InvalidDataException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:166
Thrown when RunnableInstance cannot lock an object to be used by another object due to an invalid lin...
Definition Exception.h:345
InvalidObjectLinkException(const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:347
An operation cannot be performed currently since the related object is in an invalid state like an er...
Definition Exception.h:151
InvalidStateException(std::string Description, const int ErrorCode=DynExpErrorCodes::InvalidState, const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:153
Thrown when RunnableInstance::LockObject() has not been called on an object link parameter to establi...
Definition Exception.h:330
LinkedObjectNotLockedException(const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:332
Thrown when some operation or feature is temporarily or permanently not available.
Definition Exception.h:287
NotAvailableException(std::string Description, const ErrorType Type=ErrorType::Warning, const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:289
Thrown when a requested ressource does not exist.
Definition Exception.h:237
NotFoundException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:239
Thrown when a requested feature is either under development and thus not implemented yet or when a sp...
Definition Exception.h:300
NotImplementedException(const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:302
NotImplementedException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:306
Thrown when an argument passed to a function exceeds the valid range.
Definition Exception.h:212
OutOfRangeException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:214
Thrown when a numeric operation would result in an overflow (e.g. due to incompatible data types)
Definition Exception.h:200
OverflowException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:202
Denotes that e.g. a remote gRPC service failed.
Definition Exception.h:358
ServiceFailedException(std::string Description, const ErrorType Type=ErrorType::Error, const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:360
Thrown in a multi-threading context when an answer is expected from another thread an when the commun...
Definition Exception.h:275
ThreadDidNotRespondException(const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:277
Thrown when an operation timed out before it could be completed, especially used for locking shared d...
Definition Exception.h:262
TimeoutException(std::string Description, const ErrorType Type=ErrorType::Warning, const int ErrorCode=DynExpErrorCodes::Timeout, const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:264
Thrown when an attempt was made to convert two incompatible types into each other.
Definition Exception.h:249
TypeErrorException(const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:251
Thrown when a numeric operation would result in an underflow (e.g. due to incompatible data types)
Definition Exception.h:188
UnderflowException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition Exception.h:190
Class to store information about warnings in a thread-safe manner (deriving from ILockable)....
Definition Util.h:1026
DynExpErrorCodes
DynExp's error codes
Definition Exception.h:22
DynExp's Util namespace contains commonly used functions and templates as well as extensions to Qt an...
void ForwardException(std::exception_ptr e)
Wraps the exception passed to the function in a ForwardedException and throws the ForwardedException....
Definition Exception.cpp:30
std::ostream & operator<<(std::ostream &stream, const Exception &e)
Writes a DynExp exception in a user-readable way to a stream.
Definition Exception.cpp:17
ErrorType
DynExp's error types
Definition Exception.h:15