JSON-RPC 2.0
JSON-RPC 2.0 Modern C++ Library
Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <expected>
4#include <string>
5
6#include <nlohmann/json.hpp>
7
8namespace jsonrpc::error {
9
10enum class RpcErrorCode {
11 // Standard errors
12 kParseError = -32700,
13 kInvalidRequest = -32600,
14 kMethodNotFound = -32601,
15 kInvalidParams = -32602,
16 kInternalError = -32603,
17
18 // Implementation-defined server errors
19 kServerError = -32000,
20 kTransportError = -32010,
21 kTimeoutError = -32001,
22
23 // Client errors
24 kClientError = -32099,
27
28 // Unknown error
29 kUnknownError = -32098,
30};
31
32namespace detail {
33inline auto DefaultMessageFor(RpcErrorCode code) -> std::string_view {
34 switch (code) {
36 return "Parse error";
38 return "Invalid request";
40 return "Method not found";
42 return "Invalid parameters";
44 return "Internal error";
46 return "Server error";
48 return "Transport error";
50 return "Timeout error";
52 return "Client error";
54 return "Client serialization error";
56 return "Client deserialization error";
58 return "Unknown error";
59 }
60}
61} // namespace detail
62
63// Base error type for all JSON-RPC errors
64class RpcError {
65 public:
66 RpcError(RpcErrorCode code, std::string message)
67 : code_(code), message_(std::move(message)) {
68 }
69
70 [[nodiscard]] auto to_json() const -> nlohmann::json {
71 nlohmann::json json;
72 json["code"] = static_cast<int>(Code());
73 json["message"] = Message();
74 return json;
75 }
76
77 [[nodiscard]] auto Code() const -> RpcErrorCode {
78 return code_;
79 }
80
81 [[nodiscard]] auto Message() const -> std::string {
82 return message_;
83 }
84
85 auto operator==(const RpcError& other) const -> bool {
86 return Code() == other.Code() && Message() == other.Message();
87 }
88
89 auto operator!=(const RpcError& other) const -> bool {
90 return !(*this == other);
91 }
92
93 static auto FromCode(RpcErrorCode code, std::string message = "")
94 -> RpcError {
95 if (message.empty()) {
96 message = std::string(detail::DefaultMessageFor(code));
97 }
98 return {code, std::move(message)};
99 }
100
101 static auto UnexpectedFromCode(RpcErrorCode code, std::string message = "")
102 -> std::unexpected<RpcError> {
103 return std::unexpected(RpcError::FromCode(code, std::move(message)));
104 }
105
106 private:
107 RpcErrorCode code_;
108 std::string message_;
109};
110
111inline auto Ok() -> std::expected<void, RpcError> {
112 return {};
113}
114
115} // namespace jsonrpc::error
auto operator==(const RpcError &other) const -> bool
Definition error.hpp:85
auto operator!=(const RpcError &other) const -> bool
Definition error.hpp:89
static auto FromCode(RpcErrorCode code, std::string message="") -> RpcError
Definition error.hpp:93
auto to_json() const -> nlohmann::json
Definition error.hpp:70
auto Code() const -> RpcErrorCode
Definition error.hpp:77
auto Message() const -> std::string
Definition error.hpp:81
RpcError(RpcErrorCode code, std::string message)
Definition error.hpp:66
static auto UnexpectedFromCode(RpcErrorCode code, std::string message="") -> std::unexpected< RpcError >
Definition error.hpp:101
auto DefaultMessageFor(RpcErrorCode code) -> std::string_view
Definition error.hpp:33
auto Ok() -> std::expected< void, RpcError >
Definition error.hpp:111