This repository was archived by the owner on Jan 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.go
87 lines (70 loc) · 2.14 KB
/
errors.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
package fastjsonrpc
import (
"fmt"
)
const (
renderedParseError = `{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":null}`
renderedInvalidRequest = `{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid Request"},"id":null}`
)
// ErrorCode is JSON-RPC 2.0 spec defined error code.
//
// For user defined errors it should be in range from -32099 to -32000.
type ErrorCode int
const (
errorCodeMethodNotFound ErrorCode = -32601
errorMessageMethodNotFound string = `Method not found`
errorCodeInvalidParams ErrorCode = -32602
errorMessageInvalidParams string = `Invalid params`
errorCodeInternalError ErrorCode = -32603
errorMessageInternalError string = `Internal error`
errorCodeServerError ErrorCode = -32000
errorMessageServerError string = `Server error`
)
var _ error = &Error{}
// Error is wrapper for JSON-RPC 2.0 Error Object.
type Error struct {
Code ErrorCode
Message string
Data interface{}
}
// Error implements standard error interface.
func (e *Error) Error() string {
if e.Data == nil {
return fmt.Sprintf("json-rpc error: [%d] %s", e.Code, e.Message)
}
return fmt.Sprintf("json-rpc error: [%d] %s (%+v)", e.Code, e.Message, e.Data)
}
// WithData sets error's data value.
//
// Useful with ErrServerError.
func (e *Error) WithData(data interface{}) *Error {
e.Data = data
return e
}
func errMethodNotFound() *Error {
return &Error{
Code: errorCodeMethodNotFound,
Message: errorMessageMethodNotFound,
}
}
// ErrInvalidParams returns pre-built JSON-RPC error with code -32602 and message "Invalid params".
func ErrInvalidParams() *Error {
return &Error{
Code: errorCodeInvalidParams,
Message: errorMessageInvalidParams,
}
}
// ErrInternalError returns pre-built JSON-RPC error with code -32603 and message "Internal error".
func ErrInternalError() *Error {
return &Error{
Code: errorCodeInternalError,
Message: errorMessageInternalError,
}
}
// ErrServerError returns pre-built JSON-RPC error with provided code and message "Server error".
func ErrServerError(code ErrorCode) *Error {
return &Error{
Code: code,
Message: errorMessageServerError,
}
}