-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patherrors.go
More file actions
83 lines (73 loc) · 2.45 KB
/
Copy patherrors.go
File metadata and controls
83 lines (73 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package acp
import "fmt"
// RequestError represents a JSON-RPC error with a structured error code.
//
// This type is used to return protocol-level errors from handlers with
// the correct JSON-RPC error code, matching the behavior of the
// TypeScript and Python reference SDKs.
//
// Use the factory functions (ErrParseError, ErrMethodNotFound, etc.)
// to create errors with the correct codes.
type RequestError struct {
Code ErrorCode
Msg string
Details any
}
func (e *RequestError) Error() string {
if e.Details != nil {
return fmt.Sprintf("JSON-RPC error %d: %s (details: %v)", e.Code, e.Msg, e.Details)
}
return fmt.Sprintf("JSON-RPC error %d: %s", e.Code, e.Msg)
}
// ErrParseError creates a parse error (-32700).
func ErrParseError(data any, msg ...string) *RequestError {
m := "Parse error"
if len(msg) > 0 {
m = msg[0]
}
return &RequestError{Code: ErrorCodeParseError, Msg: m, Details: data}
}
// ErrInvalidRequest creates an invalid request error (-32600).
func ErrInvalidRequest(data any, msg ...string) *RequestError {
m := "Invalid request"
if len(msg) > 0 {
m = msg[0]
}
return &RequestError{Code: ErrorCodeInvalidRequest, Msg: m, Details: data}
}
// ErrMethodNotFound creates a method not found error (-32601).
func ErrMethodNotFound(method string) *RequestError {
return &RequestError{Code: ErrorCodeMethodNotFound, Msg: fmt.Sprintf("Method not found: %s", method)}
}
// ErrInvalidParams creates an invalid params error (-32602).
func ErrInvalidParams(data any, msg ...string) *RequestError {
m := "Invalid params"
if len(msg) > 0 {
m = msg[0]
}
return &RequestError{Code: ErrorCodeInvalidParams, Msg: m, Details: data}
}
// ErrInternalError creates an internal error (-32603).
func ErrInternalError(data any, msg ...string) *RequestError {
m := "Internal error"
if len(msg) > 0 {
m = msg[0]
}
return &RequestError{Code: ErrorCodeInternalError, Msg: m, Details: data}
}
// ErrAuthRequired creates an authentication required error (-32000).
func ErrAuthRequired(data any, msg ...string) *RequestError {
m := "Authentication required"
if len(msg) > 0 {
m = msg[0]
}
return &RequestError{Code: ErrorCodeAuthenticationRequired, Msg: m, Details: data}
}
// ErrResourceNotFound creates a resource not found error (-32002).
func ErrResourceNotFound(uri ...string) *RequestError {
m := "Resource not found"
if len(uri) > 0 {
m = fmt.Sprintf("Resource not found: %s", uri[0])
}
return &RequestError{Code: ErrorCodeResourceNotFound, Msg: m}
}