-
Notifications
You must be signed in to change notification settings - Fork 19
/
response.go
178 lines (152 loc) · 5.27 KB
/
response.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package jsonrpc
import "fmt"
// Result is just a nicer way to describe what's expected to be returned by the
// handlers.
type Result interface{}
type Response struct {
// Version specifies the version of the JSON-RPC protocol.
// MUST be exactly "2.0".
Version string `json:"jsonrpc"`
// Result is REQUIRED on success. This member MUST NOT exist if there was an
// error invoking the method.
Result Result `json:"result,omitempty"`
// Error is REQUIRED on error. This member MUST NOT exist if there was no
// error triggered during invocation.
Error *ErrorDetails `json:"error,omitempty"`
// ID is an identifier established by the Client that MUST contain a String.
// This member is REQUIRED. It MUST be the same as the value of the id member
// in the Request Object.
// If there was an error in detecting the id in the Request object (e.g.
// Parse error/Invalid Request), it MUST be empty.
ID string `json:"id,omitempty"`
}
type ErrorCode int16
const (
// ErrorCodeParseError Invalid JSON was received by the server. An error
// occurred on the server while parsing the JSON text.
ErrorCodeParseError ErrorCode = -32700
// ErrorCodeInvalidRequest The JSON sent is not a valid Request object.
ErrorCodeInvalidRequest ErrorCode = -32600
// ErrorCodeMethodNotFound The method does not exist / is not available.
ErrorCodeMethodNotFound ErrorCode = -32601
// ErrorCodeInvalidParams Invalid method parameter(s).
ErrorCodeInvalidParams ErrorCode = -32602
// ErrorCodeInternalError Internal JSON-RPC error.
ErrorCodeInternalError ErrorCode = -32603
// Implementation-defined server-errors.
// -32000 to -32099 codes are reserved for implementation-defined server-errors.
// See https://www.jsonrpc.org/specification#error_object for more information.
// ErrorCodeServerError is a generic implementation-defined server error.
// This can be used when error that does not cleanly map to the error codes
// above occurs in the server.
ErrorCodeServerError ErrorCode = -32000
)
// ErrorDetails is returned when an RPC call encounters an error.
type ErrorDetails struct {
// Code indicates the error type that occurred.
Code ErrorCode `json:"code"`
// Message provides a short description of the error.
// The message SHOULD be limited to a concise single sentence.
Message string `json:"message"`
// Data is a primitive or a structured value that contains additional
// information about the error. This may be omitted.
// The value of this member is defined by the Server (e.g. detailed error
// information, nested errors etc.).
Data string `json:"data,omitempty"`
}
func (d ErrorDetails) IsInternalError() bool {
return d.Message == "Internal error"
}
func (d ErrorDetails) Error() string {
return fmt.Sprintf("%s (%s %d)", d.Data, d.Message, d.Code)
}
func NewParseError(data error) *ErrorDetails {
return &ErrorDetails{
Code: ErrorCodeParseError,
Message: "Parse error",
Data: data.Error(),
}
}
func NewInvalidRequest(data error) *ErrorDetails {
return &ErrorDetails{
Code: ErrorCodeInvalidRequest,
Message: "Invalid Request",
Data: data.Error(),
}
}
func NewMethodNotFound(methodName string) *ErrorDetails {
return &ErrorDetails{
Code: ErrorCodeMethodNotFound,
Message: "Method not found",
Data: fmt.Sprintf("method %q is not supported", methodName),
}
}
func NewUnsupportedMethod(data error) *ErrorDetails {
return &ErrorDetails{
Code: ErrorCodeMethodNotFound,
Message: "Method not found",
Data: data.Error(),
}
}
func NewInvalidParams(data error) *ErrorDetails {
return &ErrorDetails{
Code: ErrorCodeInvalidParams,
Message: "Invalid params",
Data: data.Error(),
}
}
func NewInternalError(data error) *ErrorDetails {
return &ErrorDetails{
Code: ErrorCodeInternalError,
Message: "Internal error",
Data: data.Error(),
}
}
func NewServerError(code ErrorCode, data error) *ErrorDetails {
if code > -32000 || code < -32099 {
panic("server error code should be between [-32000, -32099]")
}
return &ErrorDetails{
Code: code,
Message: "Server error",
Data: data.Error(),
}
}
func NewCustomError(code ErrorCode, message string, data error) *ErrorDetails {
if code <= -32000 {
panic("custom error code should be greater than -32000")
}
return &ErrorDetails{
Code: code,
Message: message,
Data: data.Error(),
}
}
func NewErrorResponse(id string, details *ErrorDetails) *Response {
return &Response{
Version: VERSION2,
Error: details,
ID: id,
}
}
func NewSuccessfulResponse(id string, result Result) *Response {
return &Response{
Version: VERSION2,
Result: result,
ID: id,
}
}