FOSSredder 0.5.0
Loading...
Searching...
No Matches
ErrorEventFactory.h
Go to the documentation of this file.
1
5
6#pragma once
7
10
11#include <exception>
12#include <string>
13#include <typeinfo>
14#include <utility>
15
16namespace core::errors {
17
23inline const char *severityToString(ErrorSeverity severity) noexcept {
24 switch (severity) {
26 return "info";
28 return "warning";
30 return "error";
32 return "critical";
33 }
34
35 return "error";
36}
37
47inline ErrorEvent makeExceptionEvent(ErrorSeverity severity, const char *code,
48 const char *origin,
49 std::exception_ptr exception,
50 ErrorContext context = {}) {
51 ErrorEvent event;
52 event.severity = severity;
53 event.origin = origin ? origin : std::string{};
54 event.code = code ? code : codes::ExceptionError;
55 event.context = std::move(context);
56
57 if (!exception) {
58 event.message = "unknown exception";
59 event.exceptionType = "unknown";
60 return event;
61 }
62
63 try {
64 std::rethrow_exception(exception);
65 } catch (const std::exception &ex) {
66 event.message = ex.what();
67 event.exceptionType = typeid(ex).name();
68 if (!code)
69 event.code = codes::ExceptionStd;
70 } catch (...) {
71 event.message = "non-std exception";
72 event.exceptionType = "unknown";
73 if (!code)
74 event.code = codes::ExceptionNonStd;
75 }
76
77 return event;
78}
79
80} // namespace core::errors
Declares structured error event payloads.
Defines shared structured error codes.
constexpr const char * ExceptionError
Fallback code for exception-backed errors.
constexpr const char * ExceptionNonStd
Code used when a non-standard exception is captured.
constexpr const char * ExceptionStd
Code used when a standard exception is captured.
ErrorSeverity
Severity level for structured error events.
Definition ErrorEvent.h:17
std::vector< std::pair< std::string, std::string > > ErrorContext
Key/value context attached to an error event.
Definition ErrorEvent.h:27
ErrorEvent makeExceptionEvent(ErrorSeverity severity, const char *code, const char *origin, std::exception_ptr exception, ErrorContext context={})
Build a structured event from an exception pointer.
const char * severityToString(ErrorSeverity severity) noexcept
Convert an error severity to a stable lowercase label.
Structured error payload sent through diagnostics ports.
Definition ErrorEvent.h:32