-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
82 lines (68 loc) · 2.24 KB
/
error.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
package jsonrpc
// ErrorCoder is checked by DefaultErrorEncoder. If an error value implements
// ErrorCoder, the Error will be used when encoding the error. By default,
// InternalError (-32603) is used.
type ErrorCoder interface {
ErrorCode() int
}
// Errorer describes methods for managing errors
type Errorer interface {
Error() string
ErrorCoder
}
// Error defines a JSON RPC error that can be returned
// in a Response from the spec
// http://www.jsonrpc.org/specification#error_object
type Error struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
// Error implements Errorer and error
func (e Error) Error() string {
return e.Message
}
// ErrorCode implements Errorer
func (e Error) ErrorCode() int {
return e.Code
}
const (
// ParseError defines invalid JSON was received by the server.
// An error occurred on the server while parsing the JSON text.
ParseError int = -32700
// InvalidRequestError defines the JSON sent is not a valid Request object.
InvalidRequestError int = -32600
// MethodNotFoundError defines the method does not exist / is not available.
MethodNotFoundError int = -32601
// InvalidParamsError defines invalid method parameter(s).
InvalidParamsError int = -32602
// InternalError defines a server error
InternalError int = -32603
)
var errorMessage = map[int]string{
ParseError: "An error occurred on the server while parsing the JSON text",
InvalidRequestError: "The JSON sent is not a valid Request object",
MethodNotFoundError: "The method does not exist / is not available",
InvalidParamsError: "Invalid method parameter(s)",
InternalError: "Internal JSON-RPC error",
}
// NewError returns Error struct
func NewError(code int, message ...string) Error {
msg := ErrorMessage(code)
if len(message) > 0 {
msg = message[0]
}
return Error{
Code: code,
Message: msg,
}
}
// ErrorMessage returns a message for the JSON RPC error code. It returns the empty
// string if the code is unknown.
func ErrorMessage(code int) string {
return errorMessage[code]
}
// NewInvalidParamsError is helper for returning InvalidParamsError
func NewInvalidParamsError(msg ...string) Error {
return NewError(InvalidParamsError, msg...)
}