relx 0.1.0
A Modern C++23 Type-Safe SQL Query Builder
Loading...
Searching...
No Matches
error_handling.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "../connection/connection.hpp"
4#include "../query/core.hpp"
5#include "../results/result.hpp"
6
7#include <expected>
8#include <format>
9#include <source_location>
10#include <stdexcept>
11#include <string>
12
13namespace relx {
14
18class RelxException : public std::runtime_error {
19public:
20 explicit RelxException(const std::string& message,
21 const std::source_location& location = std::source_location::current())
22 : std::runtime_error(
23 std::format("[{}:{}] {}", location.file_name(), location.line(), message)) {}
24};
25
29inline std::string format_error(const connection::ConnectionError& error) {
30 return std::format("Connection error: {} (Code: {})", error.message, error.error_code);
31}
32
36inline std::string format_error(const query::QueryError& error) {
37 return std::format("Query error: {}", error.message);
38}
39
43inline std::string format_error(const result::ResultError& error) {
44 return std::format("Result processing error: {}", error.message);
45}
46
57template <typename T, typename E>
58T& value_or_throw(std::expected<T, E>& result, const std::string& context = "",
59 const std::source_location& location = std::source_location::current()) {
60 if (!result) {
61 std::string message;
62 if (!context.empty()) {
63 message = std::format("{}: ", context);
64 }
65 message += format_error(result.error());
66 throw RelxException(message, location);
67 }
68 return result.value();
69}
70
74template <typename T, typename E>
75T value_or_throw(std::expected<T, E>&& result, const std::string& context = "",
76 const std::source_location& location = std::source_location::current()) {
77 if (!result) {
78 std::string message;
79 if (!context.empty()) {
80 message = std::format("{}: ", context);
81 }
82 message += format_error(result.error());
83 throw RelxException(message, location);
84 }
85 return std::move(result.value());
86}
87
91template <typename E>
92void throw_if_failed(const std::expected<void, E>& result, const std::string& context = "",
93 const std::source_location& location = std::source_location::current()) {
94 if (!result) {
95 std::string message;
96 if (!context.empty()) {
97 message = std::format("{}: ", context);
98 }
99 message += format_error(result.error());
100 throw RelxException(message, location);
101 }
102}
103
104} // namespace relx
Base exception class for relx errors.
RelxException(const std::string &message, const std::source_location &location=std::source_location::current())
relx database connection
std::string format_error(const connection::ConnectionError &error)
Format a ConnectionError for exception messages.
void throw_if_failed(const std::expected< void, E > &result, const std::string &context="", const std::source_location &location=std::source_location::current())
Function to check and throw on error with no return value.
T & value_or_throw(std::expected< T, E > &result, const std::string &context="", const std::source_location &location=std::source_location::current())
Function to extract a value from an expected or throw on error.
STL namespace.
Error type for database connection operations.
Error type for query operations.
Definition core.hpp:19
std::string message
Definition core.hpp:20
Error type for result processing operations.
Definition result.hpp:27