Skip to content

Commit 70b024a

Browse files
committed
jsonrpc2: fix return error to use Errorf or New
1 parent 719aa7e commit 70b024a

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

jsonrpc2.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/francoispqt/gojay"
1313
"go.uber.org/atomic"
1414
"go.uber.org/zap"
15-
"golang.org/x/xerrors"
1615
)
1716

1817
// Interface represents an interface for issuing requests that speak the JSON-RPC 2 protocol.
@@ -121,8 +120,8 @@ type pendingMap map[ID]chan *Response
121120
type handlingMap map[ID]handling
122121

123122
var (
124-
errLoadPendingMap = xerrors.New("failed to Load pendingMap")
125-
errLoadhandlingMap = xerrors.New("failed to Load handlingMap")
123+
errLoadPendingMap = New(CodeInternalError, "failed to Load pendingMap")
124+
errLoadhandlingMap = New(CodeInternalError, "failed to Load handlingMap")
126125
)
127126

128127
// NewConn creates a new connection object that reads and writes messages from
@@ -169,7 +168,7 @@ func (c *Conn) Write(p []byte) (n int, err error) {
169168
func (c *Conn) Call(ctx context.Context, method string, params, result interface{}) error {
170169
jsonParams, err := marshalToEmbedded(params)
171170
if err != nil {
172-
return xerrors.Errorf("failed to marshaling call parameters: %v", err)
171+
return Errorf(CodeParseError, "failed to marshaling call parameters: %w", err)
173172
}
174173

175174
id := ID{Number: c.seq.Add(1)}
@@ -183,7 +182,7 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
183182
// marshal the request now it is complete
184183
data, err := gojay.MarshalJSONObject(req)
185184
if err != nil {
186-
return xerrors.Errorf("failed to marshaling call request: %v", err)
185+
return Errorf(CodeParseError, "failed to marshaling call request: %w", err)
187186
}
188187
c.logger.Debug("gojay.MarshalJSONObject(req)", zap.ByteString("data", data))
189188

@@ -211,7 +210,7 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
211210
zap.Any("req.params", req.Params),
212211
)
213212
if _, err := c.stream.Write(ctx, data); err != nil {
214-
return err
213+
return Errorf(CodeInternalError, "failed to write call request data to steam: %w", err)
215214
}
216215

217216
select {
@@ -232,7 +231,7 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
232231
return nil
233232
}
234233
if err := gojay.Unsafe.Unmarshal(*resp.Result, result); err != nil {
235-
return xerrors.Errorf("failed to unmarshalling result: %v", err)
234+
return Errorf(CodeParseError, "failed to unmarshalling result: %w", err)
236235
}
237236
return nil
238237

@@ -245,7 +244,7 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
245244
// Reply sends a reply to the given request.
246245
func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err error) error {
247246
if req.IsNotify() {
248-
return xerrors.New("reply not invoked with a valid call")
247+
return New(CodeInvalidRequest, "reply not invoked with a valid call")
249248
}
250249

251250
m, ok := c.handling.Load().(handlingMap)
@@ -254,7 +253,7 @@ func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err
254253
}
255254
handling, found := m[*req.ID]
256255
if !found {
257-
return xerrors.Errorf("not a call in progress: %v", req.ID)
256+
return Errorf(CodeInternalError, "not a call in progress: %w", req.ID)
258257
}
259258

260259
elapsed := time.Since(handling.start)
@@ -280,7 +279,7 @@ func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err
280279
zap.Any("resp.Result", resp.Result),
281280
zap.Error(err),
282281
)
283-
return err
282+
return Errorf(CodeParseError, "failed to marshaling reply response: %w", err)
284283
}
285284

286285
c.logger.Debug(Send,
@@ -292,7 +291,7 @@ func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err
292291
)
293292

294293
if _, err := c.stream.Write(ctx, data); err != nil {
295-
return err
294+
return Errorf(CodeInternalError, "failed to write response data to steam: %w", err)
296295
}
297296

298297
return nil
@@ -302,7 +301,7 @@ func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err
302301
func (c *Conn) Notify(ctx context.Context, method string, params interface{}) error {
303302
prms, err := marshalToEmbedded(params)
304303
if err != nil {
305-
return Errorf(CodeParseError, "failed to marshaling notify parameters: %v", err)
304+
return Errorf(CodeParseError, "failed to marshaling notify parameters: %w", err)
306305
}
307306

308307
req := &NotificationMessage{
@@ -312,7 +311,7 @@ func (c *Conn) Notify(ctx context.Context, method string, params interface{}) er
312311
}
313312
data, err := gojay.MarshalJSONObject(req)
314313
if err != nil {
315-
return Errorf(CodeParseError, "failed to marshaling notify request: %v", err)
314+
return Errorf(CodeParseError, "failed to marshaling notify request: %w", err)
316315
}
317316

318317
c.logger.Debug(Send,
@@ -321,8 +320,11 @@ func (c *Conn) Notify(ctx context.Context, method string, params interface{}) er
321320
)
322321

323322
_, err = c.stream.Write(ctx, data)
323+
if err != nil {
324+
return Errorf(CodeInternalError, "failed to write notify request data to steam: %w", err)
325+
}
324326

325-
return err
327+
return nil
326328
}
327329

328330
// Cancel cancels a pending Call on the server side.

0 commit comments

Comments
 (0)