cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
11
12#include <chrono>
13#include <string>
14#include <utility>
15
18
19namespace mcp::errors {
20
21inline core::Error make(protocol::ErrorCode code, std::string message,
22 std::string detail = {}, std::string category = {}) {
23 return core::Error{static_cast<int>(code), std::move(message),
24 std::move(detail), std::move(category)};
25}
26
27inline core::Error parse(std::string detail = {}) {
28 return make(protocol::ErrorCode::ParseError, "parse error", std::move(detail),
29 "protocol");
30}
31
32inline core::Error invalid_request(std::string detail = {}) {
33 return make(protocol::ErrorCode::InvalidRequest, "invalid request",
34 std::move(detail), "protocol");
35}
36
37inline core::Error invalid_params(std::string detail = {}) {
38 return make(protocol::ErrorCode::InvalidParams, "invalid params",
39 std::move(detail), "protocol");
40}
41
42inline core::Error method_not_found(std::string detail = {}) {
43 return make(protocol::ErrorCode::MethodNotFound, "method not found",
44 std::move(detail), "protocol");
45}
46
47inline core::Error tool_not_found(std::string detail = {}) {
48 return make(protocol::ErrorCode::ToolNotFound, "tool not found",
49 std::move(detail), "tool");
50}
51
52inline core::Error resource_not_found(std::string detail = {}) {
53 return make(protocol::ErrorCode::ResourceNotFound, "resource not found",
54 std::move(detail), "resource");
55}
56
57inline core::Error permission_denied(std::string detail = {}) {
58 return make(protocol::ErrorCode::PermissionDenied, "permission denied",
59 std::move(detail), "permission");
60}
61
62inline core::Error rate_limited(std::string detail = {}) {
63 return make(protocol::ErrorCode::RateLimited, "rate limited",
64 std::move(detail), "rate_limit");
65}
66
67inline core::Error url_elicitation_required(std::string detail = {}) {
68 return make(protocol::ErrorCode::UrlElicitationRequired,
69 "url elicitation required", std::move(detail), "elicitation");
70}
71
72inline core::Error handler_failed(std::string detail = {}) {
73 return make(protocol::ErrorCode::InternalError, "handler failed",
74 std::move(detail), "handler");
75}
76
77inline core::Error handler_unknown_exception() {
78 return make(protocol::ErrorCode::InternalError,
79 "handler threw an unknown exception", {}, "handler");
80}
81
82inline core::Error transport_failed(std::string detail = {}) {
83 return make(protocol::ErrorCode::InternalError, "transport failed",
84 std::move(detail), "transport");
85}
86
87inline core::Error transport_closed(std::string detail = {}) {
88 return make(protocol::ErrorCode::InternalError, "transport closed",
89 std::move(detail), "transport");
90}
91
92inline core::Error transport_unexpected_response(std::string detail = {}) {
93 return make(protocol::ErrorCode::InternalError, "unexpected response",
94 std::move(detail), "transport");
95}
96
97inline core::Error transport_duplicate_request(std::string detail = {}) {
98 return make(protocol::ErrorCode::InternalError, "duplicate request id",
99 std::move(detail), "transport");
100}
101
102inline core::Error request_cancelled() {
103 return make(protocol::ErrorCode::InternalError, "request cancelled", {},
104 "cancellation");
105}
106
107inline core::Error request_timed_out(std::chrono::milliseconds timeout) {
108 return make(protocol::ErrorCode::InternalError, "request timed out",
109 std::to_string(timeout.count()) + "ms", "timeout");
110}
111
112inline core::Error request_task_missing() {
113 return make(protocol::ErrorCode::InternalError,
114 "request task is not configured", {}, "request");
115}
116
117inline core::Error request_state_missing() {
118 return make(protocol::ErrorCode::InternalError,
119 "request handle has no response state", {}, "request");
120}
121
122inline core::Error request_worker_exception(std::string detail = {}) {
123 return make(protocol::ErrorCode::InternalError,
124 "request worker threw an exception", std::move(detail),
125 "request");
126}
127
128inline core::Error request_worker_unknown_exception() {
129 return make(protocol::ErrorCode::InternalError,
130 "request worker threw an unknown exception", {}, "request");
131}
132
133inline protocol::ErrorObject to_json_rpc_error(const core::Error& error) {
134 protocol::ErrorObject object;
135 object.code = error.code;
136 object.message = error.message;
137 if (!error.detail.empty()) {
138 object.data = error.detail;
139 }
140 return object;
141}
142
143} // namespace mcp::errors
Shared JSON, JSON-RPC, error, cancellation, and progress model types.
Shared result and error primitives used by the public cxxmcp SDK.
int code
Numeric JSON-RPC error code.
Definition types.hpp:91