DocWire SDK
DocWire SDK: Deterministic, auditable, and secure data processing in C++20. On-premise by default. No compromises on computational speed.
Loading...
Searching...
No Matches
diagnostic_message.h
1/*********************************************************************************************************************************************/
2/* DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. */
3/* Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, */
4/* document analysis. Offline processing possible for security and confidentiality */
5/* */
6/* Copyright (c) SILVERCODERS Ltd, http://silvercoders.com */
7/* Project homepage: https://github.com/docwire/docwire */
8/* */
9/* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-DocWire-Commercial */
10/*********************************************************************************************************************************************/
11
12#ifndef DOCWIRE_DIAGNOSTIC_MESSAGE_H
13#define DOCWIRE_DIAGNOSTIC_MESSAGE_H
14
15#include "core_export.h"
16#include <exception>
17#include <string>
18#include <typeinfo>
19#include "error.h"
20
21namespace docwire::errors
22{
23
27inline std::string diagnostic_message(const std::exception& e)
28{
29 auto quote = [](const std::string& s) { return "\"" + s + "\""; };
30
31 std::string message;
32 try
33 {
34 std::rethrow_if_nested(e);
35 }
36 catch (const std::exception& nested_ex)
37 {
38 message = diagnostic_message(nested_ex);
39 }
40 catch (...)
41 {
42 message = "Unknown error\n";
43 }
44
45 try
46 {
47 const errors::base& error = dynamic_cast<const errors::base&>(e);
48 if (message.empty())
49 {
50 message += "Error: " + (error.context_count() > 0 ? quote(error.context_string(0)) : quote(e.what())) + "\n";
51 message += "in " + std::string{error.location.function_name()} + "\n";
52 message += "at " + std::string{error.location.file_name()} + ":" + std::to_string(error.location.line()) + "\n";
53 for (size_t i = 1; i < error.context_count(); ++i)
54 {
55 message += "with context " + quote(error.context_string(i)) + "\n";
56 }
57 }
58 else
59 {
60 message += "wrapping at: " + std::string{error.location.function_name()} + "\n";
61 message += "at " + std::string{error.location.file_name()} + ":" + std::to_string(error.location.line()) + "\n";
62 for (size_t i = 0; i < error.context_count(); ++i)
63 message += "with context " + quote(error.context_string(i)) + "\n";
64 }
65 }
66 catch (const std::bad_cast&)
67 {
68 message += "Error: " + quote(e.what()) + "\n";
69 message += "No location information available\n";
70 }
71 return message;
72}
73
77inline std::string diagnostic_message(std::exception_ptr eptr)
78{
79 try
80 {
81 std::rethrow_exception(eptr);
82 }
83 catch(const std::exception& e)
84 {
85 return diagnostic_message(e);
86 }
87 catch (...)
88 {
89 return "Unknown error\n";
90 }
91}
92
93} // namespace docwire::errors
94
95#endif // DOCWIRE_DIAGNOSTIC_MESSAGE_H
Provides features for reporting and handling errors with context data using nested exceptions.
std::string diagnostic_message(const std::exception &e)
Generates a diagnostic message for the given nested exceptions chain.
Base class for all exceptions in the SDK.
Definition error.h:76
virtual std::string context_string(size_t index) const =0
Get the string representation of the context.
virtual size_t context_count() const noexcept=0
Get the number of context items.
source_location location
The source location where the exception was thrown.
Definition error.h:78