open62541pp 0.16.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
exception.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <exception>
4#include <new> // bad_alloc
5#include <stdexcept>
6
8
9namespace opcua {
10
11/**
12 * Exception for bad status codes from open62541 `UA_STATUSCODE_*`.
13 * @see statuscodes.h
14 */
15class BadStatus : public std::exception {
16public:
18 : code_(code) {}
19
20 UA_StatusCode code() const noexcept {
21 return code_;
22 }
23
24 const char* what() const noexcept override {
25 return UA_StatusCode_name(code_);
26 }
27
28private:
29 UA_StatusCode code_;
30};
31
32/**
33 * Specific exception for open62541 status code `UA_STATUSCODE_BADDISCONNECT`.
34 * Useful to catch Client disconnects.
35 */
41
42class BadVariantAccess : public std::runtime_error {
43public:
44 using runtime_error::runtime_error; // inherit constructors
45};
46
47class CreateCertificateError : public std::runtime_error {
48public:
49 using runtime_error::runtime_error; // inherit constructors
50};
51
52namespace detail {
53
54[[nodiscard]] constexpr bool isGood(UA_StatusCode code) noexcept {
55 return (code >> 30U) == 0x00;
56}
57
58[[nodiscard]] constexpr bool isUncertain(UA_StatusCode code) noexcept {
59 return (code >> 30U) == 0x01;
60}
61
62[[nodiscard]] constexpr bool isBad(UA_StatusCode code) noexcept {
63 return (code >> 30U) >= 0x02;
64}
65
66// NOLINTNEXTLINE(performance-unnecessary-value-param)
67[[nodiscard]] inline UA_StatusCode getStatusCode(std::exception_ptr eptr) noexcept {
68 try {
69 if (eptr) {
70 std::rethrow_exception(eptr);
71 }
72 } catch (const BadStatus& e) {
73 return e.code();
74 } catch (const std::bad_alloc& /*e*/) {
76 } catch (...) {
78 }
79 return UA_STATUSCODE_GOOD;
80}
81
82} // namespace detail
83
84/**
85 * Check the status code and throw a BadStatus exception if the status code is bad.
86 */
87constexpr void throwIfBad(UA_StatusCode code) {
88 if (detail::isBad(code)) {
89 // NOLINTNEXTLINE(hicpp-multiway-paths-covered)
90 switch (code) {
92 throw BadDisconnect();
93 default:
94 throw BadStatus(code);
95 }
96 }
97}
98
99} // namespace opcua
Specific exception for open62541 status code UA_STATUSCODE_BADDISCONNECT.
Definition exception.hpp:36
Exception for bad status codes from open62541 UA_STATUSCODE_*.
Definition exception.hpp:15
BadStatus(UA_StatusCode code)
Definition exception.hpp:17
UA_StatusCode code() const noexcept
Definition exception.hpp:20
const char * what() const noexcept override
Definition exception.hpp:24
constexpr bool isGood(UA_StatusCode code) noexcept
Definition exception.hpp:54
UA_StatusCode getStatusCode(std::exception_ptr eptr) noexcept
Definition exception.hpp:67
constexpr bool isUncertain(UA_StatusCode code) noexcept
Definition exception.hpp:58
constexpr bool isBad(UA_StatusCode code) noexcept
Definition exception.hpp:62
constexpr void throwIfBad(UA_StatusCode code)
Check the status code and throw a BadStatus exception if the status code is bad.
Definition exception.hpp:87
#define UA_STATUSCODE_BADINTERNALERROR
#define UA_STATUSCODE_BADOUTOFMEMORY
#define UA_STATUSCODE_GOOD
#define UA_STATUSCODE_BADDISCONNECT
UA_EXPORT const char * UA_StatusCode_name(UA_StatusCode code)
uint32_t UA_StatusCode