unilink  0.4.3
A simple C++ library for unified async communication
exceptions.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2025 Jinwoo Sung
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <stdexcept>
20 #include <string>
21 
23 
24 namespace unilink {
25 namespace diagnostics {
26 
33 class UNILINK_API UnilinkException : public std::runtime_error {
34  public:
35  explicit UnilinkException(const std::string& message, const std::string& component = "",
36  const std::string& operation = "")
37  : std::runtime_error(message), component_(component), operation_(operation) {}
38 
39  const std::string& get_component() const noexcept { return component_; }
40  const std::string& get_operation() const noexcept { return operation_; }
41 
42  std::string get_full_message() const {
43  std::string full_msg = what();
44  if (!component_.empty()) {
45  full_msg = "[" + component_ + "] " + full_msg;
46  }
47  if (!operation_.empty()) {
48  full_msg += " (operation: " + operation_ + ")";
49  }
50  return full_msg;
51  }
52 
53  private:
54  std::string component_;
55  std::string operation_;
56 };
57 
65  public:
66  explicit BuilderException(const std::string& message, const std::string& builder_type = "",
67  const std::string& operation = "")
68  : UnilinkException(message, "builder", operation), builder_type_(builder_type) {}
69 
70  const std::string& get_builder_type() const noexcept { return builder_type_; }
71 
72  std::string get_full_message() const {
73  std::string full_msg = UnilinkException::get_full_message();
74  if (!builder_type_.empty()) {
75  full_msg = "[" + builder_type_ + "] " + full_msg;
76  }
77  return full_msg;
78  }
79 
80  private:
81  std::string builder_type_;
82 };
83 
91  public:
92  explicit ValidationException(const std::string& message, const std::string& parameter = "",
93  const std::string& expected = "")
94  : UnilinkException(message, "validation", "validate"), parameter_(parameter), expected_(expected) {}
95 
96  const std::string& get_parameter() const noexcept { return parameter_; }
97  const std::string& get_expected() const noexcept { return expected_; }
98 
99  std::string get_full_message() const {
100  std::string full_msg = UnilinkException::get_full_message();
101  if (!parameter_.empty()) {
102  full_msg += " (parameter: " + parameter_ + ")";
103  }
104  if (!expected_.empty()) {
105  full_msg += " (expected: " + expected_ + ")";
106  }
107  return full_msg;
108  }
109 
110  private:
111  std::string parameter_;
112  std::string expected_;
113 };
114 
122  public:
123  explicit MemoryException(const std::string& message, size_t size = 0, const std::string& operation = "")
124  : UnilinkException(message, "memory", operation), size_(size) {}
125 
126  size_t get_size() const noexcept { return size_; }
127 
128  std::string get_full_message() const {
129  std::string full_msg = UnilinkException::get_full_message();
130  if (size_ > 0) {
131  full_msg += " (size: " + std::to_string(size_) + " bytes)";
132  }
133  return full_msg;
134  }
135 
136  private:
137  size_t size_;
138 };
139 
147  public:
148  explicit ConnectionException(const std::string& message, const std::string& connection_type = "",
149  const std::string& operation = "")
150  : UnilinkException(message, "connection", operation), connection_type_(connection_type) {}
151 
152  const std::string& get_connection_type() const noexcept { return connection_type_; }
153 
154  std::string get_full_message() const {
155  std::string full_msg = UnilinkException::get_full_message();
156  if (!connection_type_.empty()) {
157  full_msg = "[" + connection_type_ + "] " + full_msg;
158  }
159  return full_msg;
160  }
161 
162  private:
163  std::string connection_type_;
164 };
165 
173  public:
174  explicit ConfigurationException(const std::string& message, const std::string& config_section = "",
175  const std::string& operation = "")
176  : UnilinkException(message, "configuration", operation), config_section_(config_section) {}
177 
178  const std::string& get_config_section() const noexcept { return config_section_; }
179 
180  std::string get_full_message() const {
181  std::string full_msg = UnilinkException::get_full_message();
182  if (!config_section_.empty()) {
183  full_msg += " (section: " + config_section_ + ")";
184  }
185  return full_msg;
186  }
187 
188  private:
189  std::string config_section_;
190 };
191 
192 } // namespace diagnostics
193 
194 // Compatibility alias while transitioning from legacy `common` namespace.
195 namespace common {
202 } // namespace common
203 } // namespace unilink
#define UNILINK_API
Definition: visibility.hpp:37