/
error.go
62 lines (51 loc) · 2.18 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
// 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
import "fmt"
// 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"`
}
// 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}
}
// Error implements the error interface.
func (e Error) Error() string {
s := fmt.Sprintf("jsonrpc2.Error{Code:%v, Message:%#v", e.Code, e.Message)
if e.Data != nil {
s += fmt.Sprintf(", Data:%#v", e.Data)
}
return s + "}"
}
// Official JSON-RPC 2.0 Errors
// InvalidParams returns a pointer to a new Error initialized with the
// InvalidParamsCode and InvalidParamsMessage and the user provided data.
// MethodFuncs are responsible for detecting and returning this error.
func InvalidParams(data interface{}) *Error {
return NewError(InvalidParamsCode, InvalidParamsMessage, data)
}
// internalError returns a pointer to a new Error initialized with the
// InternalErrorCode and InternalErrorMessage and the user provided data.
func internalError(data interface{}) *Error {
return NewError(InternalErrorCode, InternalErrorMessage, data)
}
// parseError returns a pointer to a new Error initialized with the
// ParseErrorCode and ParseErrorMessage and the user provided data.
func parseError(data interface{}) *Error {
return NewError(ParseErrorCode, ParseErrorMessage, data)
}
// invalidRequest returns a pointer to a new Error initialized with the
// InvalidRequestCode and InvalidRequestMessage and the user provided data.
func invalidRequest(data interface{}) *Error {
return NewError(InvalidRequestCode, InvalidRequestMessage, data)
}
// methodNotFound returns a pointer to a new Error initialized with the
// MethodNotFoundCode and MethodNotFoundMessage and the user provided data.
func methodNotFound(data interface{}) *Error {
return NewError(MethodNotFoundCode, MethodNotFoundMessage, data)
}