DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
Exception.cpp
Go to the documentation of this file.
1#include "stdafx.h"
2#include "Exception.h"
3
4// This file is part of DynExp.
5
6namespace Util
7{
8 Exception::Exception(std::string Description, const ErrorType Type, const int ErrorCode, const std::source_location Location) noexcept
9 : std::runtime_error(std::move(Description)), Type(Type), ErrorCode(ErrorCode),
10 Line(Location.line()), Function(Location.function_name()), File(Location.file_name())
11#ifdef DYNEXP_HAS_STACKTRACE
12 , Trace(std::stacktrace::current())
13#endif // DYNEXP_HAS_STACKTRACE
14 {
15 }
16
17 std::ostream& operator<<(std::ostream& stream, const Exception& e)
18 {
19 stream << CurrentTimeAndDateString() << " - " << e.GetErrorLabel();
20
21 auto Filename = FilenameFromPath(e.File);
22 if (!Filename.empty() || !e.Function.empty())
23 stream << " ";
24
25 stream << EventLogger::FormatLog(e.what(), e.Line, e.Function, Filename, e.ErrorCode);
26
27 return stream;
28 }
29
30 void ForwardException(std::exception_ptr e)
31 {
32 try
33 {
34 if (e)
35 std::rethrow_exception(e);
36 }
37 catch (const Exception& e)
38 {
39 std::throw_with_nested(ForwardedException(e));
40 }
41 catch (const std::exception& e)
42 {
43 std::throw_with_nested(ForwardedException(e));
44 }
45 catch (...)
46 {
47 std::throw_with_nested(ForwardedException());
48 }
49 }
50}
Provides exception type and error definitions used by DynExp.
static std::string FormatLog(const std::string &Message, const size_t Line=0, const std::string &Function="", const std::string &Filename="", const int ErrorCode=0, const bool PrefixMessage=true)
Formats a log entry as plain text to be displayed within DynExp.
Definition Util.cpp:365
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
const size_t Line
Line in source code where the exception occurred.
Definition Exception.h:107
static constexpr const char * GetErrorLabel(const ErrorType Type)
Converts an error type to a user-readable label for logging.
Definition Exception.h:71
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 int ErrorCode
DynExp error code from DynExpErrorCodes::DynExpErrorCodes
Definition Exception.h:106
Class to forward an Exception instance from one DynExp::Object instance to another DynExp::Object ins...
Definition Exception.h:126
DynExp's Util namespace contains commonly used functions and templates as well as extensions to Qt an...
auto FilenameFromPath(std::string Path)
Extracts the filename from a path.
Definition Util.h:846
void ForwardException(std::exception_ptr e)
Wraps the exception passed to the function in a ForwardedException and throws the ForwardedException....
Definition Exception.cpp:30
auto CurrentTimeAndDateString()
Returns a human-readable string describing the current time and date in the current time zone.
Definition Util.h:839
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
Accumulates include statements to provide a precompiled header.