VLink  2.0.0
A high-performance communication middleware
exception.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2026 by Thun Lu. All rights reserved.
3  * Author: Thun Lu <thun.lu@zohomail.cn>
4  * Repo: https://github.com/thun-res/vlink
5  * _ __ __ _ __
6  * | | / / / / (_) ____ / /__
7  * | | / / / / / / / __ \ / //_/
8  * | |/ / / /___ / / / / / / / ,<
9  * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 
24 /**
25  * @file exception.h
26  * @brief Thin @c final wrappers around the standard exception hierarchy used by VLink.
27  *
28  * @details
29  * Each VLink exception type is a @c final subclass of the corresponding standard exception base
30  * and lives inside the @c vlink::Exception namespace to avoid clashing with application code.
31  * Constructors and @c what() are inherited verbatim; only @c OperationCancelled overrides
32  * @c what() with a fixed identifier string.
33  *
34  * @par Class hierarchy
35  *
36  * | VLink class | Standard base | Typical usage |
37  * | --------------------------------- | --------------------------- | -------------------------------------- |
38  * | @c Exception::RuntimeError | @c std::runtime_error | Generic runtime failure (logger fatal) |
39  * | @c Exception::OutOfRange | @c std::out_of_range | Index or iterator out of bounds |
40  * | @c Exception::InvalidArgument | @c std::invalid_argument | Bad function argument |
41  * | @c Exception::LogicError | @c std::logic_error | Violated precondition |
42  * | @c Exception::DomainError | @c std::domain_error | Value outside the function domain |
43  * | @c Exception::LengthError | @c std::length_error | Exceeded implementation limit |
44  * | @c Exception::RangeError | @c std::range_error | Arithmetic range error |
45  * | @c Exception::OverflowError | @c std::overflow_error | Arithmetic overflow |
46  * | @c Exception::UnderflowError | @c std::underflow_error | Arithmetic underflow |
47  * | @c Exception::OperationCancelled | @c std::exception | Cooperative cancellation observed |
48  *
49  * @note
50  * @c Logger throws @c Exception::RuntimeError whenever a @c kFatal message is emitted. The
51  * logger flushes all sinks before throwing so the application can catch the exception and
52  * shut down cleanly.
53  *
54  * @par Example
55  * @code
56  * try {
57  * VLOG_F("Critical failure: ", reason);
58  * } catch (const vlink::Exception::RuntimeError& e) {
59  * std::cerr << e.what() << "\n";
60  * shutdown_cleanup();
61  * }
62  * @endcode
63  */
64 
65 #pragma once
66 
67 #include <exception>
68 #include <stdexcept>
69 
70 #include "./macros.h"
71 
72 #if defined(_WIN32) || defined(__CYGWIN__)
73 #define VLINK_EXCEPTION_EXPORT
74 #else
75 #define VLINK_EXCEPTION_EXPORT VLINK_EXPORT
76 #endif
77 
78 namespace vlink {
79 
80 /**
81  * @namespace vlink::Exception
82  * @brief Container namespace for VLink exception types.
83  */
84 namespace Exception { // NOLINT(readability-identifier-naming)
85 
86 /**
87  * @class RuntimeError
88  * @brief Generic runtime failure; thrown by the logger on @c kFatal messages.
89  */
90 class VLINK_EXCEPTION_EXPORT RuntimeError final : public std::runtime_error {
91  public:
92  using std::runtime_error::runtime_error;
93 };
94 
95 /**
96  * @class OutOfRange
97  * @brief Indicates an index or iterator that is outside the legal range.
98  */
99 class VLINK_EXCEPTION_EXPORT OutOfRange final : public std::out_of_range {
100  public:
101  using std::out_of_range::out_of_range;
102 };
103 
104 /**
105  * @class InvalidArgument
106  * @brief Indicates that a function received an argument with an invalid value.
107  */
108 class VLINK_EXCEPTION_EXPORT InvalidArgument final : public std::invalid_argument {
109  public:
110  using std::invalid_argument::invalid_argument;
111 };
112 
113 /**
114  * @class LogicError
115  * @brief Indicates a violated program logic precondition.
116  */
117 class VLINK_EXCEPTION_EXPORT LogicError final : public std::logic_error {
118  public:
119  using std::logic_error::logic_error;
120 };
121 
122 /**
123  * @class DomainError
124  * @brief Indicates a value outside the mathematical domain of a function.
125  */
126 class VLINK_EXCEPTION_EXPORT DomainError final : public std::domain_error {
127  public:
128  using std::domain_error::domain_error;
129 };
130 
131 /**
132  * @class LengthError
133  * @brief Indicates an attempt to exceed an implementation size limit.
134  */
135 class VLINK_EXCEPTION_EXPORT LengthError final : public std::length_error {
136  public:
137  using std::length_error::length_error;
138 };
139 
140 /**
141  * @class RangeError
142  * @brief Indicates an arithmetic range error.
143  */
144 class VLINK_EXCEPTION_EXPORT RangeError final : public std::range_error {
145  public:
146  using std::range_error::range_error;
147 };
148 
149 /**
150  * @class OverflowError
151  * @brief Indicates an arithmetic overflow.
152  */
153 class VLINK_EXCEPTION_EXPORT OverflowError final : public std::overflow_error {
154  public:
155  using std::overflow_error::overflow_error;
156 };
157 
158 /**
159  * @class UnderflowError
160  * @brief Indicates an arithmetic underflow.
161  */
162 class VLINK_EXCEPTION_EXPORT UnderflowError final : public std::underflow_error {
163  public:
164  using std::underflow_error::underflow_error;
165 };
166 
167 /**
168  * @class OperationCancelled
169  * @brief Marker exception thrown when a cooperative cancellation request is observed.
170  */
171 class VLINK_EXCEPTION_EXPORT OperationCancelled final : public std::exception {
172  public:
173  using std::exception::exception;
174 
175  /**
176  * @brief Returns a fixed explanatory message identifying the exception.
177  *
178  * @return Static null-terminated string.
179  */
180  [[nodiscard]] const char* what() const noexcept override { return "vlink operation cancelled"; }
181 };
182 
183 } // namespace Exception
184 
185 } // namespace vlink
186 
187 #undef VLINK_EXCEPTION_EXPORT
#define VLINK_EXCEPTION_EXPORT
Definition: exception.h:75
Cross-platform macros for visibility, branch hints, copy prevention, singletons and string helpers.