Skip to content

Commit 886e09e

Browse files
committed
error: use golang.org/x/xerror
1 parent ed4a612 commit 886e09e

File tree

1 file changed

+59
-17
lines changed

1 file changed

+59
-17
lines changed

error.go

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,40 @@ package jsonrpc2
66

77
import (
88
"fmt"
9+
10+
"golang.org/x/xerrors"
911
)
1012

13+
// Code represents a error's category.
1114
type Code int64
1215

1316
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
2327

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.
2533
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
2739
)
2840

29-
// Error
41+
// Error represents a jsonrpc2 error.
3042
type Error struct {
31-
3243
// Code a number indicating the error type that occurred.
3344
Code Code `json:"code"`
3445

@@ -38,20 +49,51 @@ type Error struct {
3849

3950
// Message a string providing a short description of the error.
4051
Message string `json:"message"`
52+
53+
frame xerrors.Frame
54+
err error
4155
}
4256

57+
// Error implements error.
4358
func (e *Error) Error() string {
4459
if e == nil {
4560
return ""
4661
}
4762
return e.Message
4863
}
4964

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+
5089
// 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,
5593
Message: fmt.Sprintf(format, args...),
94+
frame: xerrors.Caller(0),
5695
}
96+
e.err = xerrors.New(e.Message)
97+
98+
return e
5799
}

0 commit comments

Comments
 (0)