Senzing C++ SDK
C++ SDK for Senzing v4, patterned after the official C# SDK
Loading...
Searching...
No Matches
SzException.hpp
Go to the documentation of this file.
1// SzException.hpp -- exception hierarchy for the Senzing C++ SDK.
2//
3// Ported from the official C# SDK (senzing-garage/sz-sdk-csharp@4.3.0,
4// Senzing.Sdk/SzException.cs and the subclass tree). Names mirror the C# SDK
5// character-for-character. The root SzException derives from std::exception and
6// carries an optional numeric Senzing error code (mirroring C# `long? ErrorCode`).
7//
8// SzEnvironmentDestroyedException is deliberately NOT part of this tree: in C#
9// it derives from InvalidOperationException (a misuse/logic error, not a
10// Senzing engine error), so here it derives from std::logic_error.
11#ifndef SENZING_SDK_SZEXCEPTION_HPP
12#define SENZING_SDK_SZEXCEPTION_HPP
13
14#include <cstdint>
15#include <exception>
16#include <optional>
17#include <stdexcept>
18#include <string>
19
20namespace senzing::sdk {
21
27class SzException : public std::exception {
28public:
29 SzException() = default;
30
31 explicit SzException(std::string message)
32 : message_(std::move(message)) {}
33
34 SzException(std::optional<int64_t> errorCode, std::string message)
35 : message_(std::move(message)), errorCode_(errorCode) {}
36
37 explicit SzException(const std::exception& cause)
38 : message_(cause.what()) {}
39
40 SzException(std::string message, const std::exception& cause)
41 : message_(std::move(message) + ": " + cause.what()) {}
42
43 SzException(std::optional<int64_t> errorCode, std::string message,
44 const std::exception& cause)
45 : message_(std::move(message) + ": " + cause.what()),
46 errorCode_(errorCode) {}
47
48 ~SzException() override = default;
49
52 [[nodiscard]] std::optional<int64_t> ErrorCode() const noexcept {
53 return errorCode_;
54 }
55
56 [[nodiscard]] const char* what() const noexcept override {
57 return message_.c_str();
58 }
59
60protected:
61 std::string message_;
62 std::optional<int64_t> errorCode_;
63};
64
65// Macro to declare an SzException subclass that re-exposes the six-constructor
66// surface and forwards to the named base. Keeps the tree faithful to C# while
67// avoiding hand-repeated boilerplate.
68#define SENZING_SDK_DECLARE_EXCEPTION(Name, Base) \
69 class Name : public Base { \
70 public: \
71 Name() = default; \
72 explicit Name(std::string message) : Base(std::move(message)) {} \
73 Name(std::optional<int64_t> errorCode, std::string message) \
74 : Base(errorCode, std::move(message)) {} \
75 explicit Name(const std::exception& cause) : Base(cause) {} \
76 Name(std::string message, const std::exception& cause) \
77 : Base(std::move(message), cause) {} \
78 Name(std::optional<int64_t> errorCode, std::string message, \
79 const std::exception& cause) \
80 : Base(errorCode, std::move(message), cause) {} \
81 }
82
83// ---- Tier 1: direct children of SzException ----
85SENZING_SDK_DECLARE_EXCEPTION(SzConfigurationException, SzException);
86SENZING_SDK_DECLARE_EXCEPTION(SzReplaceConflictException, SzException);
88SENZING_SDK_DECLARE_EXCEPTION(SzUnrecoverableException, SzException);
89
90// ---- Tier 2: children of SzBadInputException ----
91SENZING_SDK_DECLARE_EXCEPTION(SzNotFoundException, SzBadInputException);
92SENZING_SDK_DECLARE_EXCEPTION(SzUnknownDataSourceException, SzBadInputException);
93
94// ---- Tier 2: children of SzRetryableException ----
95SENZING_SDK_DECLARE_EXCEPTION(SzDatabaseConnectionLostException,
96 SzRetryableException);
97SENZING_SDK_DECLARE_EXCEPTION(SzDatabaseTransientException,
98 SzRetryableException);
99SENZING_SDK_DECLARE_EXCEPTION(SzRetryTimeoutExceededException,
100 SzRetryableException);
101
102// ---- Tier 2: children of SzUnrecoverableException ----
103SENZING_SDK_DECLARE_EXCEPTION(SzDatabaseException, SzUnrecoverableException);
104SENZING_SDK_DECLARE_EXCEPTION(SzLicenseException, SzUnrecoverableException);
105SENZING_SDK_DECLARE_EXCEPTION(SzNotInitializedException,
106 SzUnrecoverableException);
107SENZING_SDK_DECLARE_EXCEPTION(SzUnhandledException, SzUnrecoverableException);
108
109#undef SENZING_SDK_DECLARE_EXCEPTION
110
115class SzEnvironmentDestroyedException : public std::logic_error {
116public:
118 : std::logic_error("The Senzing environment has been destroyed.") {}
119 explicit SzEnvironmentDestroyedException(const std::string& message)
120 : std::logic_error(message) {}
121 // Mirrors the C# (string message, Exception cause) and (Exception cause)
122 // constructors. std::logic_error carries no separate cause, so -- exactly as
123 // SzException does -- the cause's text is folded into the message.
124 SzEnvironmentDestroyedException(const std::string& message,
125 const std::exception& cause)
126 : std::logic_error(message + ": " + cause.what()) {}
127 explicit SzEnvironmentDestroyedException(const std::exception& cause)
128 : std::logic_error(cause.what()) {}
129};
130
131} // namespace senzing::sdk
132
133#endif // SENZING_SDK_SZEXCEPTION_HPP
#define SENZING_SDK_DECLARE_EXCEPTION(Name, Base)
Definition SzException.hpp:68
Thrown when an SzEnvironment (or a subsystem it owns) is used after Destroy().
Definition SzException.hpp:115
SzEnvironmentDestroyedException(const std::exception &cause)
Definition SzException.hpp:127
SzEnvironmentDestroyedException(const std::string &message)
Definition SzException.hpp:119
SzEnvironmentDestroyedException()
Definition SzException.hpp:117
SzEnvironmentDestroyedException(const std::string &message, const std::exception &cause)
Definition SzException.hpp:124
Base exception for all Senzing engine errors.
Definition SzException.hpp:27
std::string message_
Definition SzException.hpp:61
const char * what() const noexcept override
Definition SzException.hpp:56
SzException(std::string message)
Definition SzException.hpp:31
SzException(std::optional< int64_t > errorCode, std::string message, const std::exception &cause)
Definition SzException.hpp:43
SzException(std::string message, const std::exception &cause)
Definition SzException.hpp:40
SzException(const std::exception &cause)
Definition SzException.hpp:37
std::optional< int64_t > errorCode_
Definition SzException.hpp:62
~SzException() override=default
std::optional< int64_t > ErrorCode() const noexcept
The underlying Senzing error code, or std::nullopt if none was associated with this exception.
Definition SzException.hpp:52
SzException(std::optional< int64_t > errorCode, std::string message)
Definition SzException.hpp:34
Definition SzCoreConfig.hpp:20