Common System 1.0.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
error.cppm
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
27module;
28
29#include <string>
30#include <string_view>
31
32export module kcenon.common:error;
33
34export namespace kcenon::common::error {
35
40enum class category : int {
41 success = 0,
42 common = -1,
43 thread_system = -100,
44 logger_system = -200,
45 monitoring_system = -300,
46 container_system = -400,
47 database_system = -500,
48 network_system = -600,
49 pacs_system = -700,
50};
51
56namespace codes {
57
58// ============================================================================
59// Common Error Codes (-1 to -99)
60// ============================================================================
61namespace common_errors {
62 constexpr int success = 0;
63 constexpr int invalid_argument = -1;
64 constexpr int not_found = -2;
65 constexpr int permission_denied = -3;
66 constexpr int timeout = -4;
67 constexpr int cancelled = -5;
68 constexpr int not_initialized = -6;
69 constexpr int already_exists = -7;
70 constexpr int out_of_memory = -8;
71 constexpr int io_error = -9;
72 constexpr int network_error = -10;
73 constexpr int registry_frozen = -11;
74 constexpr int internal_error = -99;
75
76 // DI (dependency injection) errors (-50 to -59)
77 constexpr int di_service_not_registered = -50;
78 constexpr int di_circular_dependency = -51;
79 constexpr int di_already_registered = -52;
80 constexpr int di_factory_error = -53;
81 constexpr int di_invalid_lifetime = -54;
82 constexpr int di_scoped_from_root = -55;
83} // namespace common_errors
84
85// ============================================================================
86// thread_system Error Codes (-100 to -199)
87// ============================================================================
88namespace thread_system {
89 constexpr int base = static_cast<int>(category::thread_system);
90
91 // Pool errors (-100 to -119)
92 constexpr int pool_full = base - 0;
93 constexpr int pool_shutdown = base - 1;
94 constexpr int pool_not_started = base - 2;
95 constexpr int invalid_pool_size = base - 3;
96
97 // Worker errors (-120 to -139)
98 constexpr int worker_failed = base - 20;
99 constexpr int worker_not_found = base - 21;
100 constexpr int worker_busy = base - 22;
101
102 // Job errors (-140 to -159)
103 constexpr int job_rejected = base - 40;
104 constexpr int job_timeout = base - 41;
105 constexpr int job_cancelled = base - 42;
106 constexpr int invalid_job = base - 43;
107
108 // Queue errors (-160 to -179)
109 constexpr int queue_full = base - 60;
110 constexpr int queue_empty = base - 61;
111 constexpr int queue_stopped = base - 62;
112} // namespace thread_system
113
114// ============================================================================
115// logger_system Error Codes (-200 to -299)
116// ============================================================================
117namespace logger_system {
118 constexpr int base = static_cast<int>(category::logger_system);
119
120 // File errors (-200 to -219)
121 constexpr int file_open_failed = base - 0;
122 constexpr int file_write_failed = base - 1;
123 constexpr int file_close_failed = base - 2;
124 constexpr int file_rotation_failed = base - 3;
125 constexpr int file_permission_denied = base - 4;
126
127 // Writer errors (-220 to -239)
128 constexpr int writer_not_initialized = base - 20;
129 constexpr int writer_stopped = base - 21;
130 constexpr int writer_full = base - 22;
131 constexpr int async_writer_failed = base - 23;
132
133 // Format errors (-240 to -259)
134 constexpr int invalid_format = base - 40;
135 constexpr int format_error = base - 41;
136
137 // Filter errors (-260 to -279)
138 constexpr int invalid_filter = base - 60;
139 constexpr int filter_rejected = base - 61;
140} // namespace logger_system
141
142// ============================================================================
143// monitoring_system Error Codes (-300 to -399)
144// ============================================================================
145namespace monitoring_system {
146 constexpr int base = static_cast<int>(category::monitoring_system);
147
148 // Metric errors (-300 to -319)
149 constexpr int metric_not_found = base - 0;
150 constexpr int invalid_metric_type = base - 1;
151 constexpr int metric_collection_failed = base - 2;
152
153 // Storage errors (-320 to -339)
154 constexpr int storage_full = base - 20;
155 constexpr int storage_error = base - 21;
156
157 // Event errors (-340 to -359)
158 constexpr int event_publish_failed = base - 40;
159 constexpr int event_subscribe_failed = base - 41;
160 constexpr int invalid_event_type = base - 42;
161
162 // Profiler errors (-360 to -379)
163 constexpr int profiler_not_enabled = base - 60;
164 constexpr int profiler_error = base - 61;
165} // namespace monitoring_system
166
167// ============================================================================
168// container_system Error Codes (-400 to -499)
169// ============================================================================
170namespace container_system {
171 constexpr int base = static_cast<int>(category::container_system);
172
173 // Value errors (-400 to -419)
174 constexpr int value_type_mismatch = base - 0;
175 constexpr int invalid_value_type = base - 1;
176 constexpr int value_conversion_failed = base - 2;
177
178 // Serialization errors (-420 to -439)
179 constexpr int serialization_failed = base - 20;
180 constexpr int deserialization_failed = base - 21;
181 constexpr int invalid_format = base - 22;
182
183 // Memory pool errors (-440 to -459)
184 constexpr int pool_exhausted = base - 40;
185 constexpr int allocation_failed = base - 41;
186 constexpr int invalid_allocation_size = base - 42;
187
188 // Container errors (-460 to -479)
189 constexpr int key_not_found = base - 60;
190 constexpr int duplicate_key = base - 61;
191 constexpr int container_full = base - 62;
192} // namespace container_system
193
194// ============================================================================
195// database_system Error Codes (-500 to -599)
196// ============================================================================
197namespace database_system {
198 constexpr int base = static_cast<int>(category::database_system);
199
200 // Connection errors (-500 to -519)
201 constexpr int connection_failed = base - 0;
202 constexpr int connection_lost = base - 1;
203 constexpr int connection_timeout = base - 2;
204 constexpr int invalid_connection_string = base - 3;
205
206 // Pool errors (-520 to -539)
207 constexpr int pool_exhausted = base - 20;
208 constexpr int pool_shutdown = base - 21;
209 constexpr int pool_timeout = base - 22;
210
211 // Query errors (-540 to -559)
212 constexpr int query_failed = base - 40;
213 constexpr int query_syntax_error = base - 41;
214 constexpr int query_timeout = base - 42;
215
216 // Transaction errors (-560 to -579)
217 constexpr int transaction_failed = base - 60;
218 constexpr int transaction_rolled_back = base - 61;
219 constexpr int transaction_timeout = base - 62;
220} // namespace database_system
221
222// ============================================================================
223// network_system Error Codes (-600 to -699)
224// ============================================================================
225namespace network_system {
226 constexpr int base = static_cast<int>(category::network_system);
227
228 // Connection errors (-600 to -619)
229 constexpr int connection_failed = base - 0;
230 constexpr int connection_refused = base - 1;
231 constexpr int connection_timeout = base - 2;
232 constexpr int connection_closed = base - 3;
233
234 // Session errors (-620 to -639)
235 constexpr int session_not_found = base - 20;
236 constexpr int session_expired = base - 21;
237 constexpr int invalid_session = base - 22;
238
239 // Send/Receive errors (-640 to -659)
240 constexpr int send_failed = base - 40;
241 constexpr int receive_failed = base - 41;
242 constexpr int message_too_large = base - 42;
243
244 // Server errors (-660 to -679)
245 constexpr int server_not_started = base - 60;
246 constexpr int server_already_running = base - 61;
247 constexpr int bind_failed = base - 62;
248} // namespace network_system
249
250// ============================================================================
251// pacs_system Error Codes (-700 to -799)
252// ============================================================================
253namespace pacs_system {
254 constexpr int base = static_cast<int>(category::pacs_system);
255
256 // DICOM file errors (-700 to -719)
257 constexpr int file_not_found = base - 0;
258 constexpr int file_read_error = base - 1;
259 constexpr int file_write_error = base - 2;
260 constexpr int invalid_dicom_file = base - 3;
261 constexpr int missing_dicm_prefix = base - 4;
262 constexpr int invalid_meta_info = base - 5;
263 constexpr int missing_transfer_syntax = base - 6;
264 constexpr int unsupported_transfer_syntax = base - 7;
265
266 // DICOM element errors (-720 to -739)
267 constexpr int element_not_found = base - 20;
268 constexpr int value_conversion_error = base - 21;
269 constexpr int invalid_vr = base - 22;
270 constexpr int invalid_tag = base - 23;
271 constexpr int data_size_mismatch = base - 24;
272
273 // Encoding/Decoding errors (-740 to -759)
274 constexpr int decode_error = base - 40;
275 constexpr int encode_error = base - 41;
276 constexpr int compression_error = base - 42;
277 constexpr int decompression_error = base - 43;
278
279 // Network/Association errors (-760 to -779)
280 constexpr int association_rejected = base - 60;
281 constexpr int association_aborted = base - 61;
282 constexpr int dimse_error = base - 62;
283 constexpr int pdu_error = base - 63;
284
285 // Storage errors (-780 to -799)
286 constexpr int storage_failed = base - 80;
287 constexpr int retrieve_failed = base - 81;
288 constexpr int query_failed = base - 82;
289} // namespace pacs_system
290
291} // namespace codes
292
298inline std::string_view get_error_message(int code) {
299 switch (code) {
300 // Common errors
301 case codes::common_errors::success: return "Success";
302 case codes::common_errors::invalid_argument: return "Invalid argument";
303 case codes::common_errors::not_found: return "Not found";
304 case codes::common_errors::permission_denied: return "Permission denied";
305 case codes::common_errors::timeout: return "Timeout";
306 case codes::common_errors::cancelled: return "Cancelled";
307 case codes::common_errors::not_initialized: return "Not initialized";
308 case codes::common_errors::already_exists: return "Already exists";
309 case codes::common_errors::out_of_memory: return "Out of memory";
310 case codes::common_errors::io_error: return "I/O error";
311 case codes::common_errors::network_error: return "Network error";
312 case codes::common_errors::registry_frozen: return "Registry is frozen";
313 case codes::common_errors::internal_error: return "Internal error";
314
315 // DI errors
316 case codes::common_errors::di_service_not_registered: return "Service not registered in container";
317 case codes::common_errors::di_circular_dependency: return "Circular dependency detected";
318 case codes::common_errors::di_already_registered: return "Service already registered";
319 case codes::common_errors::di_factory_error: return "Factory error during instantiation";
320 case codes::common_errors::di_invalid_lifetime: return "Invalid service lifetime configuration";
321 case codes::common_errors::di_scoped_from_root: return "Scoped service resolved from root container";
322
323 // thread_system errors
324 case codes::thread_system::pool_full: return "Thread pool full";
325 case codes::thread_system::pool_shutdown: return "Thread pool shutdown";
326 case codes::thread_system::job_rejected: return "Job rejected";
327 case codes::thread_system::job_timeout: return "Job timeout";
328
329 // logger_system errors
330 case codes::logger_system::file_open_failed: return "Failed to open log file";
331 case codes::logger_system::file_write_failed: return "Failed to write to log file";
332 case codes::logger_system::file_rotation_failed: return "Log file rotation failed";
333
334 // monitoring_system errors
335 case codes::monitoring_system::metric_not_found: return "Metric not found";
336 case codes::monitoring_system::storage_full: return "Metric storage full";
337
338 // container_system errors
339 case codes::container_system::value_type_mismatch: return "Value type mismatch";
340 case codes::container_system::serialization_failed: return "Serialization failed";
341 case codes::container_system::pool_exhausted: return "Memory pool exhausted";
342
343 // database_system errors
344 case codes::database_system::connection_failed: return "Database connection failed";
345 case codes::database_system::pool_exhausted: return "Connection pool exhausted";
346 case codes::database_system::query_failed: return "Database query failed";
347
348 // network_system errors
349 case codes::network_system::connection_failed: return "Network connection failed";
350 case codes::network_system::send_failed: return "Network send failed";
351 case codes::network_system::server_not_started: return "Server not started";
352
353 // pacs_system errors
354 case codes::pacs_system::file_not_found: return "DICOM file not found";
355 case codes::pacs_system::file_read_error: return "Failed to read DICOM file";
356 case codes::pacs_system::file_write_error: return "Failed to write DICOM file";
357 case codes::pacs_system::invalid_dicom_file: return "Invalid DICOM file format";
358 case codes::pacs_system::missing_dicm_prefix: return "Missing DICM prefix";
359 case codes::pacs_system::invalid_meta_info: return "Invalid File Meta Information";
360 case codes::pacs_system::missing_transfer_syntax: return "Missing Transfer Syntax";
361 case codes::pacs_system::unsupported_transfer_syntax: return "Unsupported Transfer Syntax";
362 case codes::pacs_system::element_not_found: return "DICOM element not found";
363 case codes::pacs_system::value_conversion_error: return "Value conversion failed";
364 case codes::pacs_system::decode_error: return "DICOM decode error";
365 case codes::pacs_system::encode_error: return "DICOM encode error";
366 case codes::pacs_system::association_rejected: return "DICOM association rejected";
367 case codes::pacs_system::storage_failed: return "DICOM storage failed";
368
369 default: return "Unknown error";
370 }
371}
372
378inline std::string_view get_category_name(int code) {
379 if (code == codes::common_errors::success) return "Success";
380 // Positive codes are out of range: this registry assigns categories only
381 // to negative error codes, so any code > 0 is not a valid error code.
382 if (code > 0) return "Invalid";
383 if (code > static_cast<int>(category::thread_system)) return "Common";
384 if (code > static_cast<int>(category::logger_system)) return "ThreadSystem";
385 if (code > static_cast<int>(category::monitoring_system)) return "LoggerSystem";
386 if (code > static_cast<int>(category::container_system)) return "MonitoringSystem";
387 if (code > static_cast<int>(category::database_system)) return "ContainerSystem";
388 if (code > static_cast<int>(category::network_system)) return "DatabaseSystem";
389 if (code > static_cast<int>(category::pacs_system)) return "NetworkSystem";
390 return "PACSSystem";
391}
392
393} // namespace kcenon::common::error
Specific error codes organized by category.
std::string_view get_error_message(int code)
Get human-readable error message for error code.
category
Error code category ranges for compile-time validation.
Definition error_codes.h:37
std::string_view get_category_name(int code)
Get category name for error code.