DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Exception.h
Go to the documentation of this file.
1 // This file is part of DynExp.
2 
8 #pragma once
9 
10 namespace Util
11 {
15  enum class ErrorType { Info, Warning, Error, Fatal };
16 
17  namespace DynExpErrorCodes
18  {
22  enum DynExpErrorCodes : int {
23  NoError = 0,
25  InvalidArg = -2,
29  Underflow = -6,
30  Overflow = -7,
31  OutOfRange = -8,
32  Empty = -9,
33  NotFound = -10,
34  TypeError = -11,
35  Timeout = -12,
37  NotAvailable = -14,
39  FileIOError = -16,
42  ServiceFailed = -19
43  };
44  }
45 
50  class Exception : public std::runtime_error
51  {
52  public:
62  Exception(std::string Description, const ErrorType Type = ErrorType::Error, const int ErrorCode = -1,
63  const std::source_location Location = std::source_location::current()) noexcept;
64  virtual ~Exception() = default;
65 
71  constexpr static const char* GetErrorLabel(const ErrorType Type)
72  {
73  switch (Type)
74  {
75  case ErrorType::Warning: return "Warning";
76  case ErrorType::Error: return "Error";
77  case ErrorType::Fatal: return "Fatal Error";
78  default: return "Info";
79  }
80  }
81 
87  constexpr static const char* GetErrorLabelColor(const ErrorType Type)
88  {
89  switch (Type)
90  {
91  case ErrorType::Warning: return "Orange";
92  case ErrorType::Error: return "Red";
93  case ErrorType::Fatal: return "DarkRed";
94  default: return "Blue";
95  }
96  }
97 
98  constexpr const char* GetErrorLabel() const { return GetErrorLabel(Type); }
99  constexpr const char* GetErrorLabelColor() const { return GetErrorLabelColor(Type); }
100 
101 #ifdef DYNEXP_HAS_STACKTRACE
102  auto& GetStackTrace() const { return Trace; }
103 #endif // DYNEXP_HAS_STACKTRACE
104 
105  const ErrorType Type;
106  const int ErrorCode;
107  const size_t Line;
108  const std::string Function;
109  const std::string File;
110 
111  private:
112 #ifdef DYNEXP_HAS_STACKTRACE
116  const std::stacktrace Trace;
117 #endif // DYNEXP_HAS_STACKTRACE
118  };
119 
126  {
127  public:
128  ForwardedException() noexcept : Exception("Unknown forwarded exception") {}
129  ForwardedException(const std::exception& e) noexcept : Exception(e.what()) {}
130  ForwardedException(const Exception& e) noexcept : Exception(e) {}
131  };
132 
137  {
138  public:
139  InvalidArgException(std::string Description,
140  const std::source_location Location = std::source_location::current()) noexcept
141  : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::InvalidArg, Location)
142  {}
143  };
144 
150  {
151  public:
152  InvalidStateException(std::string Description, const int ErrorCode = DynExpErrorCodes::InvalidState,
153  const std::source_location Location = std::source_location::current()) noexcept
154  : Exception(std::move(Description), ErrorType::Error, ErrorCode, Location)
155  {}
156  };
157 
163  {
164  public:
165  InvalidDataException(std::string Description,
166  const std::source_location Location = std::source_location::current()) noexcept
167  : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::InvalidData, Location)
168  {}
169  };
170 
175  {
176  public:
177  InvalidCallException(std::string Description,
178  const std::source_location Location = std::source_location::current()) noexcept
179  : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::InvalidCall, Location)
180  {}
181  };
182 
187  {
188  public:
189  UnderflowException(std::string Description,
190  const std::source_location Location = std::source_location::current()) noexcept
191  : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::Underflow, Location)
192  {}
193  };
194 
199  {
200  public:
201  OverflowException(std::string Description,
202  const std::source_location Location = std::source_location::current()) noexcept
203  : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::Overflow, Location)
204  {}
205  };
206 
211  {
212  public:
213  OutOfRangeException(std::string Description,
214  const std::source_location Location = std::source_location::current()) noexcept
215  : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::OutOfRange, Location)
216  {}
217  };
218 
223  class EmptyException : public Exception
224  {
225  public:
226  EmptyException(std::string Description,
227  const std::source_location Location = std::source_location::current()) noexcept
228  : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::Empty, Location)
229  {}
230  };
231 
236  {
237  public:
238  NotFoundException(std::string Description,
239  const std::source_location Location = std::source_location::current()) noexcept
240  : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::NotFound, Location)
241  {}
242  };
243 
248  {
249  public:
250  TypeErrorException(const std::source_location Location = std::source_location::current()) noexcept
251  : Exception("A base pointer did not belong to the expected derived type.",
252  ErrorType::Error, DynExpErrorCodes::TypeError, Location)
253  {}
254  };
255 
261  {
262  public:
264  const std::source_location Location = std::source_location::current()) noexcept
265  : Exception(std::move(Description), Type, ErrorCode, Location)
266  {}
267  };
268 
274  {
275  public:
276  ThreadDidNotRespondException(const std::source_location Location = std::source_location::current()) noexcept
277  : TimeoutException("Timeout occurred. A thread did not respond. Retry or terminate the program.",
279  {}
280  };
281 
286  {
287  public:
288  NotAvailableException(std::string Description, const ErrorType Type = ErrorType::Warning,
289  const std::source_location Location = std::source_location::current()) noexcept
290  : Exception(std::move(Description), Type, DynExpErrorCodes::NotAvailable, Location)
291  {}
292  };
293 
299  {
300  public:
301  NotImplementedException(const std::source_location Location = std::source_location::current()) noexcept
302  : Exception("This function has not been implemented for the given item type.",
303  ErrorType::Error, DynExpErrorCodes::NotImplemented, Location)
304  {}
305  NotImplementedException(std::string Description,
306  const std::source_location Location = std::source_location::current()) noexcept
307  : Exception(std::move(Description), ErrorType::Error, DynExpErrorCodes::NotImplemented, Location)
308  {}
309  };
310 
315  {
316  public:
317  FileIOErrorException(std::string Filename,
318  const std::source_location Location = std::source_location::current()) noexcept
319  : Exception("Error reading from or writing to file \"" + Filename + "\".",
320  ErrorType::Error, DynExpErrorCodes::FileIOError, Location)
321  {}
322  };
323 
329  {
330  public:
331  LinkedObjectNotLockedException(const std::source_location Location = std::source_location::current()) noexcept
333  "A linked object wrapper container is empty. Probably, the respective object link parameter has not been locked calling RunnableInstance::LockObject().",
335  {}
336  };
337 
344  {
345  public:
346  InvalidObjectLinkException(const std::source_location Location = std::source_location::current()) noexcept
348  "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  {}
351  };
352 
357  {
358  public:
359  ServiceFailedException(std::string Description, const ErrorType Type = ErrorType::Error,
360  const std::source_location Location = std::source_location::current()) noexcept
361  : Exception(std::move(Description), Type, DynExpErrorCodes::ServiceFailed, Location)
362  {}
363  };
364 
371  std::ostream& operator<<(std::ostream& stream, const Exception& e);
372 
378  void ForwardException(std::exception_ptr e);
379 }
Thrown when a list is expected to contain entries and when a query results in an empty answer or an e...
Definition: Exception.h:224
EmptyException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:226
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:109
constexpr static const char * GetErrorLabel(const ErrorType Type)
Converts an error type to a user-readable label for logging.
Definition: Exception.h:71
const size_t Line
Line in source code where the exception occurred.
Definition: Exception.h:107
constexpr const char * GetErrorLabel() const
Definition: Exception.h:98
Exception(std::string Description, const ErrorType Type=ErrorType::Error, const int ErrorCode=-1, const std::source_location Location=std::source_location::current()) noexcept
Constructs an exception. Constructor is noexcept, although std::runtime_error() might throw std::bad_...
Definition: Exception.cpp:8
const std::string Function
Function in source code where the exception occurred
Definition: Exception.h:108
const ErrorType Type
DynExp error type from Util::ErrorType
Definition: Exception.h:105
constexpr static const char * GetErrorLabelColor(const ErrorType Type)
Converts an error type to an HTML color name for logging.
Definition: Exception.h:87
virtual ~Exception()=default
const int ErrorCode
DynExp error code from DynExpErrorCodes::DynExpErrorCodes
Definition: Exception.h:106
constexpr const char * GetErrorLabelColor() const
Definition: Exception.h:99
Thrown when reading from or writing to a file failed.
Definition: Exception.h:315
FileIOErrorException(std::string Filename, const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:317
Class to forward an Exception instance from one DynExp::Object instance to another DynExp::Object ins...
Definition: Exception.h:126
ForwardedException() noexcept
Definition: Exception.h:128
ForwardedException(const Exception &e) noexcept
Definition: Exception.h:130
ForwardedException(const std::exception &e) noexcept
Definition: Exception.h:129
An invalid argument like a null pointer has been passed to a function.
Definition: Exception.h:137
InvalidArgException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:139
Thrown when a function call is not allowed to a specific thread in a multi-threading context.
Definition: Exception.h:175
InvalidCallException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:177
Data to operate on is invalid for a specific purpose. This indicates a corrupted data structure or fu...
Definition: Exception.h:163
InvalidDataException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:165
Thrown when RunnableInstance cannot lock an object to be used by another object due to an invalid lin...
Definition: Exception.h:344
InvalidObjectLinkException(const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:346
An operation cannot be performed currently since the related object is in an invalid state like an er...
Definition: Exception.h:150
InvalidStateException(std::string Description, const int ErrorCode=DynExpErrorCodes::InvalidState, const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:152
Thrown when RunnableInstance::LockObject() has not been called on an object link parameter to establi...
Definition: Exception.h:329
LinkedObjectNotLockedException(const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:331
Thrown when some operation or feature is temporarily or permanently not available.
Definition: Exception.h:286
NotAvailableException(std::string Description, const ErrorType Type=ErrorType::Warning, const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:288
Thrown when a requested ressource does not exist.
Definition: Exception.h:236
NotFoundException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:238
Thrown when a requested feature is either under development and thus not implemented yet or when a sp...
Definition: Exception.h:299
NotImplementedException(const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:301
NotImplementedException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:305
Thrown when an argument passed to a function exceeds the valid range.
Definition: Exception.h:211
OutOfRangeException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:213
Thrown when a numeric operation would result in an overflow (e.g. due to incompatible data types)
Definition: Exception.h:199
OverflowException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:201
Denotes that e.g. a remote gRPC service failed.
Definition: Exception.h:357
ServiceFailedException(std::string Description, const ErrorType Type=ErrorType::Error, const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:359
Thrown in a multi-threading context when an answer is expected from another thread an when the commun...
Definition: Exception.h:274
ThreadDidNotRespondException(const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:276
Thrown when an operation timed out before it could be completed, especially used for locking shared d...
Definition: Exception.h:261
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:263
Thrown when an attempt was made to convert two incompatible types into each other.
Definition: Exception.h:248
TypeErrorException(const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:250
Thrown when a numeric operation would result in an underflow (e.g. due to incompatible data types)
Definition: Exception.h:187
UnderflowException(std::string Description, const std::source_location Location=std::source_location::current()) noexcept
Definition: Exception.h:189
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...
Definition: circularbuf.cpp:7
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