-
Notifications
You must be signed in to change notification settings - Fork 7
/
error.go
70 lines (61 loc) · 2.61 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
// Copyright 2018 Adam S Levy. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package jsonrpc2
// Error represents a JSON-RPC 2.0 Error object, which is used in the Response
// object.
type Error struct {
Code ErrorCode `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
// ErrorCode represent the int JSON RPC 2.0 error code.
type ErrorCode int
// Official JSON-RPC 2.0 Spec Error Codes and Messages
const (
LowestReservedErrorCode ErrorCode = -32768
ParseErrorCode ErrorCode = -32700
InvalidRequestCode ErrorCode = -32600
MethodNotFoundCode ErrorCode = -32601
InvalidParamsCode ErrorCode = -32602
InternalErrorCode ErrorCode = -32603
HighestReservedErrorCode ErrorCode = -32000
ParseErrorMessage = "Parse error"
InvalidRequestMessage = "Invalid Request"
MethodNotFoundMessage = "Method not found"
InvalidParamsMessage = "Invalid params"
InternalErrorMessage = "Internal error"
)
// IsReserved returns true if c is within the reserved error code range:
// [LowestReservedErrorCode, HighestReservedErrorCode].
func (c ErrorCode) IsReserved() bool {
return LowestReservedErrorCode <= c && c <= HighestReservedErrorCode
}
// Official JSON-RPC 2.0 Errors
var (
// ParseError is returned to the client if a JSON is not well formed.
ParseError = NewError(ParseErrorCode, ParseErrorMessage, nil)
// InvalidRequest is returned to the client if a request does not
// conform to JSON-RPC 2.0 spec
InvalidRequest = NewError(InvalidRequestCode, InvalidRequestMessage, nil)
// MethodNotFound is returned to the client if a method is called that
// has not been registered with RegisterMethod()
MethodNotFound = NewError(MethodNotFoundCode, MethodNotFoundMessage, nil)
// InvalidParams is returned to the client if a method is called with
// an invalid "params" object. A method's function is responsible for
// detecting and returning this error.
InvalidParams = NewError(InvalidParamsCode, InvalidParamsMessage, nil)
// InternalError is returned to the client if a method function returns
// an invalid response object.
InternalError = NewError(InternalErrorCode, InternalErrorMessage, nil)
)
// NewError returns an Error with the given code, message, and data.
func NewError(code ErrorCode, message string, data interface{}) Error {
return Error{Code: code, Message: message, Data: data}
}
// NewInvalidParamsError returns an InvalidParams Error with the given data.
func NewInvalidParamsError(data interface{}) Error {
err := InvalidParams
err.Data = data
return err
}