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
error.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_ERROR_H
13#define DOCWIRE_ERROR_H
14
15#include "core_export.h"
16#include "diagnostic_context.h" // IWYU pragma: keep
17#include <exception>
18#include <typeinfo>
19#include "serialization_pair.h" // IWYU pragma: keep
20#include "source_location.h"
21#include "type_id.h"
22#include <tuple>
23#include <utility>
24
29namespace docwire::errors
30{
31#ifdef DOCWIRE_ENABLE_SHORT_MACRO_NAMES
32 #define current_location DOCWIRE_CURRENT_LOCATION
33#endif
34
75struct DOCWIRE_CORE_EXPORT base : public std::exception
76{
79
85 base(const source_location& location = source_location::current());
86 base(const base&);
87 base(base&&);
88 base& operator=(const base&);
89 base& operator=(base&&);
90 ~base() override;
91
99 virtual type_id context_type_id(size_t index) const noexcept = 0;
100
108 virtual std::string context_string(size_t index) const = 0;
109
113 virtual size_t context_count() const noexcept = 0;
114
124 const char* what() const noexcept override;
125};
126
127} // namespace docwire::errors
128
129namespace docwire::errors
130{
131std::string diagnostic_message(const std::exception& e);
132std::string diagnostic_message(std::exception_ptr eptr);
133} // namespace docwire::errors
134
135#include "stringification.h"
136
137namespace docwire::errors
138{
139
166template <typename... T>
167struct impl : public base
168{
169private:
170 template<size_t I>
171 std::string context_string_impl() const
172 {
173 return stringify(std::get<I>(context));
174 }
175
176 template<size_t I>
177 type_id context_type_impl() const noexcept
178 {
180 }
181
182 template <size_t... Is>
183 std::string context_string_at(size_t index, std::index_sequence<Is...>) const {
184 using FuncType = std::string(impl::*)() const;
185 static constexpr FuncType funcs[] = { &impl::template context_string_impl<Is>... };
186 return (this->*funcs[index])();
187 }
188
189 template <size_t... Is>
190 type_id context_type_at(size_t index, std::index_sequence<Is...>) const noexcept {
191 using FuncType = type_id(impl::*)() const noexcept;
192 static constexpr FuncType funcs[] = { &impl::template context_type_impl<Is>... };
193 return (this->*funcs[index])();
194 }
195
196public:
200 std::tuple<T...> context;
201
208 explicit impl(const std::tuple<T...>& context_tuple, const source_location& location = source_location::current())
209 : base(location), context(context_tuple)
210 {
211 }
212
221 type_id context_type_id(size_t index) const noexcept override
222 {
223 return context_type_at(index, std::make_index_sequence<sizeof...(T)>{});
224 }
225
234 std::string context_string(size_t index) const override
235 {
236 return context_string_at(index, std::make_index_sequence<sizeof...(T)>{});
237 }
238
243 size_t context_count() const noexcept override
244 {
245 return sizeof...(T);
246 }
247};
248
249} // namespace docwire::errors
250
251#endif
Strong type representing a compile-time type identity.
Definition type_id.h:18
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.
The main namespace for the DocWire SDK.
Definition ai_elements.h:19
basic_source_location source_location
A type that describes a location in source code. Uses a custom fallback implementation.
consteval type_id type_id_of() noexcept
Returns a compile-time strong identifier for type T.
Definition type_id.h:68
virtual type_id context_type_id(size_t index) const noexcept=0
Get the type information of the context.
base(const source_location &location=source_location::current())
Constructs a base object with the current source location.
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
const char * what() const noexcept override
Get the exception type.
std::tuple< T... > context
A tuple holding all context items provided when the error was created.
Definition error.h:200
size_t context_count() const noexcept override
Get the number of context items.
Definition error.h:243
type_id context_type_id(size_t index) const noexcept override
Get the type information of the context.
Definition error.h:221
std::string context_string(size_t index) const override
Get the string representation of the context.
Definition error.h:234
impl(const std::tuple< T... > &context_tuple, const source_location &location=source_location::current())
Constructs an error object from a tuple of context items.
Definition error.h:208