Skip to content

Commit 6bff20f

Browse files
committed
error: remove xerrors dependency
1 parent b18b1f8 commit 6bff20f

File tree

2 files changed

+25
-47
lines changed

2 files changed

+25
-47
lines changed

error.go

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ package jsonrpc2
66

77
import (
88
"encoding/json"
9+
"errors"
910
"fmt"
10-
11-
"golang.org/x/xerrors"
1211
)
1312

1413
// Code represents a error's category.
@@ -66,62 +65,41 @@ type Error struct {
6665
// information about the error. Can be omitted.
6766
Data *json.RawMessage `json:"data"`
6867

69-
frame xerrors.Frame
70-
err error
68+
err error
7169
}
7270

73-
// Error implements error.
71+
// compile time check whether the Error implements error interface.
72+
var _ error = (*Error)(nil)
73+
74+
// Error implements error.Error.
7475
func (e *Error) Error() string {
7576
if e == nil {
7677
return ""
7778
}
7879
return e.Message
7980
}
8081

81-
// Format implements fmt.Formatter.
82-
func (e *Error) Format(s fmt.State, c rune) {
83-
xerrors.FormatError(e, s, c)
84-
}
85-
86-
// FormatError implements xerrors.Formatter.
87-
func (e *Error) FormatError(p xerrors.Printer) (next error) {
88-
if e.Message == "" {
89-
p.Printf("code=%v", e.Code)
90-
} else {
91-
p.Printf("%s (code=%v)", e.Message, e.Code)
92-
}
93-
e.frame.Format(p)
94-
95-
return e.err
96-
}
97-
98-
// Unwrap implements xerrors.Wrapper.
82+
// Unwrap implements errors.Unwrap.
9983
//
100-
// The returns the error underlying the receiver, which may be nil.
101-
func (e *Error) Unwrap() error {
102-
return e.err
103-
}
84+
// Returns the error underlying the receiver, which may be nil.
85+
func (e *Error) Unwrap() error { return e.err }
10486

10587
// NewError builds a Error struct for the suppied message and code.
106-
func NewError(c Code, args ...interface{}) *Error {
107-
e := &Error{
88+
func NewError(c Code, message string) *Error {
89+
return &Error{
10890
Code: c,
109-
Message: fmt.Sprint(args...),
110-
frame: xerrors.Caller(1),
91+
Message: message,
92+
err: errors.New(message),
11193
}
112-
e.err = xerrors.New(e.Message)
113-
114-
return e
11594
}
11695

11796
// Errorf builds a Error struct for the suppied message and code.
11897
func Errorf(c Code, format string, args ...interface{}) *Error {
11998
e := &Error{
120-
Code: c,
121-
Message: fmt.Sprintf(format, args...),
122-
frame: xerrors.Caller(1),
99+
Code: c,
100+
err: fmt.Errorf(format, args...),
123101
}
124-
e.err = xerrors.New(e.Message)
102+
e.Message = e.err.Error()
125103

126104
return e
127105
}

error_gojay.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ package jsonrpc2
88

99
import "github.com/francoispqt/gojay"
1010

11+
// MarshalJSONObject implements gojay's MarshalerJSONObject
12+
func (e *Error) MarshalJSONObject(enc *gojay.Encoder) {
13+
enc.IntKey(keyCode, int(e.Code))
14+
enc.StringKey(keyMessage, e.Message)
15+
}
16+
17+
// IsNil returns wether the structure is nil value or not
18+
func (e *Error) IsNil() bool { return e == nil }
19+
1120
// UnmarshalJSONObject implements gojay's UnmarshalerJSONObject
1221
func (e *Error) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
1322
switch k {
@@ -22,15 +31,6 @@ func (e *Error) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
2231
// NKeys returns the number of keys to unmarshal
2332
func (e *Error) NKeys() int { return 2 }
2433

25-
// MarshalJSONObject implements gojay's MarshalerJSONObject
26-
func (e *Error) MarshalJSONObject(enc *gojay.Encoder) {
27-
enc.IntKey(keyCode, int(e.Code))
28-
enc.StringKey(keyMessage, e.Message)
29-
}
30-
31-
// IsNil returns wether the structure is nil value or not
32-
func (e *Error) IsNil() bool { return e == nil }
33-
3434
// compile time check whether the Error implements a gojay.MarshalerJSONObject interface.
3535
var _ gojay.MarshalerJSONObject = (*Error)(nil)
3636

0 commit comments

Comments
 (0)