@@ -12,7 +12,6 @@ import (
12
12
"github.com/francoispqt/gojay"
13
13
"go.uber.org/atomic"
14
14
"go.uber.org/zap"
15
- "golang.org/x/xerrors"
16
15
)
17
16
18
17
// Interface represents an interface for issuing requests that speak the JSON-RPC 2 protocol.
@@ -121,8 +120,8 @@ type pendingMap map[ID]chan *Response
121
120
type handlingMap map [ID ]handling
122
121
123
122
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" )
126
125
)
127
126
128
127
// 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) {
169
168
func (c * Conn ) Call (ctx context.Context , method string , params , result interface {}) error {
170
169
jsonParams , err := marshalToEmbedded (params )
171
170
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 )
173
172
}
174
173
175
174
id := ID {Number : c .seq .Add (1 )}
@@ -183,7 +182,7 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
183
182
// marshal the request now it is complete
184
183
data , err := gojay .MarshalJSONObject (req )
185
184
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 )
187
186
}
188
187
c .logger .Debug ("gojay.MarshalJSONObject(req)" , zap .ByteString ("data" , data ))
189
188
@@ -211,7 +210,7 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
211
210
zap .Any ("req.params" , req .Params ),
212
211
)
213
212
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 )
215
214
}
216
215
217
216
select {
@@ -232,7 +231,7 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
232
231
return nil
233
232
}
234
233
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 )
236
235
}
237
236
return nil
238
237
@@ -245,7 +244,7 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
245
244
// Reply sends a reply to the given request.
246
245
func (c * Conn ) Reply (ctx context.Context , req * Request , result interface {}, err error ) error {
247
246
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" )
249
248
}
250
249
251
250
m , ok := c .handling .Load ().(handlingMap )
@@ -254,7 +253,7 @@ func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err
254
253
}
255
254
handling , found := m [* req .ID ]
256
255
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 )
258
257
}
259
258
260
259
elapsed := time .Since (handling .start )
@@ -280,7 +279,7 @@ func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err
280
279
zap .Any ("resp.Result" , resp .Result ),
281
280
zap .Error (err ),
282
281
)
283
- return err
282
+ return Errorf ( CodeParseError , "failed to marshaling reply response: %w" , err )
284
283
}
285
284
286
285
c .logger .Debug (Send ,
@@ -292,7 +291,7 @@ func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err
292
291
)
293
292
294
293
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 )
296
295
}
297
296
298
297
return nil
@@ -302,7 +301,7 @@ func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err
302
301
func (c * Conn ) Notify (ctx context.Context , method string , params interface {}) error {
303
302
prms , err := marshalToEmbedded (params )
304
303
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 )
306
305
}
307
306
308
307
req := & NotificationMessage {
@@ -312,7 +311,7 @@ func (c *Conn) Notify(ctx context.Context, method string, params interface{}) er
312
311
}
313
312
data , err := gojay .MarshalJSONObject (req )
314
313
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 )
316
315
}
317
316
318
317
c .logger .Debug (Send ,
@@ -321,8 +320,11 @@ func (c *Conn) Notify(ctx context.Context, method string, params interface{}) er
321
320
)
322
321
323
322
_ , 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
+ }
324
326
325
- return err
327
+ return nil
326
328
}
327
329
328
330
// Cancel cancels a pending Call on the server side.
0 commit comments