@@ -6,29 +6,40 @@ package jsonrpc2
6
6
7
7
import (
8
8
"fmt"
9
+
10
+ "golang.org/x/xerrors"
9
11
)
10
12
13
+ // Code represents a error's category.
11
14
type Code int64
12
15
13
16
const (
14
- CodeParseError Code = - 32700
15
- CodeInvalidRequest Code = - 32600
16
- CodeMethodNotFound Code = - 32601
17
- CodeInvalidParams Code = - 32602
18
- CodeInternalError Code = - 32603
19
- CodeServerErrorStart Code = - 32099
20
- CodeServerErrorEnd Code = - 32000
21
- CodeServerNotInitialized Code = - 32002
22
- CodeUnknownErrorCode Code = - 32001
17
+ // CodeParseError is the invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
18
+ CodeParseError Code = - 32700
19
+ // CodeInvalidRequest is the JSON sent is not a valid Request object.
20
+ CodeInvalidRequest Code = - 32600
21
+ // CodeMethodNotFound is the method does not exist / is not available.
22
+ CodeMethodNotFound Code = - 32601
23
+ // CodeInvalidParams is the invalid method parameter(s).
24
+ CodeInvalidParams Code = - 32602
25
+ // CodeInternalError is the internal JSON-RPC error.
26
+ CodeInternalError Code = - 32603
23
27
24
- // Defined by the protocol.
28
+ // CodeServerNotInitialized is the error of server not initialized.
29
+ CodeServerNotInitialized Code = - 32002
30
+ // CodeUnknownError should be used for all non coded errors.
31
+ CodeUnknownError Code = - 32001
32
+ // CodeRequestCancelled is the cancellation error.
25
33
CodeRequestCancelled Code = - 32800
26
- CodeContentModified Code = - 32801
34
+ // CodeContentModified is the state change that invalidates the result of a request in execution.
35
+ CodeContentModified Code = - 32801
36
+
37
+ codeServerErrorStart Code = - 32099
38
+ codeServerErrorEnd Code = - 32000
27
39
)
28
40
29
- // Error
41
+ // Error represents a jsonrpc2 error.
30
42
type Error struct {
31
-
32
43
// Code a number indicating the error type that occurred.
33
44
Code Code `json:"code"`
34
45
@@ -38,20 +49,51 @@ type Error struct {
38
49
39
50
// Message a string providing a short description of the error.
40
51
Message string `json:"message"`
52
+
53
+ frame xerrors.Frame
54
+ err error
41
55
}
42
56
57
+ // Error implements error.
43
58
func (e * Error ) Error () string {
44
59
if e == nil {
45
60
return ""
46
61
}
47
62
return e .Message
48
63
}
49
64
65
+ // Format implements fmt.Formatter.
66
+ func (e * Error ) Format (s fmt.State , c rune ) {
67
+ xerrors .FormatError (e , s , c )
68
+ }
69
+
70
+ // FormatError implements xerrors.Formatter.
71
+ func (e * Error ) FormatError (p xerrors.Printer ) (next error ) {
72
+ if e .Message == "" {
73
+ p .Printf ("code=%v" , e .Code )
74
+ } else {
75
+ p .Printf ("%s (code=%v)" , e .Message , e .Code )
76
+ }
77
+ e .frame .Format (p )
78
+
79
+ return e .err
80
+ }
81
+
82
+ // Unwrap implements xerrors.Wrapper.
83
+ //
84
+ // The returns the error underlying the receiver, which may be nil.
85
+ func (e * Error ) Unwrap () error {
86
+ return e .err
87
+ }
88
+
50
89
// Errorf builds a Error struct for the suppied message and code.
51
- // If args is not empty, message and args will be passed to Sprintf.
52
- func Errorf (code Code , format string , args ... interface {}) * Error {
53
- return & Error {
54
- Code : code ,
90
+ func Errorf (c Code , format string , args ... interface {}) * Error {
91
+ e := & Error {
92
+ Code : c ,
55
93
Message : fmt .Sprintf (format , args ... ),
94
+ frame : xerrors .Caller (0 ),
56
95
}
96
+ e .err = xerrors .New (e .Message )
97
+
98
+ return e
57
99
}
0 commit comments