Skip to content

Commit 9c5b41b

Browse files
committed
error: remove Code prefix
1 parent 85f679e commit 9c5b41b

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed

error.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,32 @@ import (
1515
type Code int64
1616

1717
const (
18-
// CodeParseError is the invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
19-
CodeParseError Code = -32700
20-
// CodeInvalidRequest is the JSON sent is not a valid Request object.
21-
CodeInvalidRequest Code = -32600
22-
// CodeMethodNotFound is the method does not exist / is not available.
23-
CodeMethodNotFound Code = -32601
24-
// CodeInvalidParams is the invalid method parameter(s).
25-
CodeInvalidParams Code = -32602
26-
// CodeInternalError is the internal JSON-RPC error.
27-
CodeInternalError Code = -32603
28-
29-
// CodeServerNotInitialized is the error of server not initialized.
30-
CodeServerNotInitialized Code = -32002
31-
// CodeUnknownError should be used for all non coded errors.
32-
CodeUnknownError Code = -32001
33-
// CodeRequestCancelled is the cancellation error.
34-
CodeRequestCancelled Code = -32800
35-
// CodeContentModified is the state change that invalidates the result of a request in execution.
36-
CodeContentModified Code = -32801
37-
38-
// CodeServerOverloaded is returned when a message was refused due to a
18+
// ParseError is the invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
19+
ParseError = Code(-32700)
20+
// InvalidRequest is the JSON sent is not a valid Request object.
21+
InvalidRequest = Code(-32600)
22+
// MethodNotFound is the method does not exist / is not available.
23+
MethodNotFound = Code(-32601)
24+
// InvalidParams is the invalid method parameter(s).
25+
InvalidParams = Code(-32602)
26+
// InternalError is the internal JSON-RPC error.
27+
InternalError = Code(-32603)
28+
29+
// ServerNotInitialized is the error of server not initialized.
30+
ServerNotInitialized = Code(-32002)
31+
// UnknownError should be used for all non coded errors.
32+
UnknownError = Code(-32001)
33+
// RequestCancelled is the cancellation error.
34+
RequestCancelled = Code(-32800)
35+
// ContentModified is the state change that invalidates the result of a request in execution.
36+
ContentModified = Code(-32801)
37+
38+
// ServerOverloaded is returned when a message was refused due to a
3939
// server being temporarily unable to accept any new messages.
40-
CodeServerOverloaded = -32000
40+
ServerOverloaded = Code(-32000)
4141

42-
codeServerErrorStart Code = -32099 //nolint:deadcode,varcheck
43-
codeServerErrorEnd Code = -32000 //nolint:deadcode,varcheck
42+
codeServerErrorStart = Code(-32099) //nolint:deadcode,varcheck
43+
codeServerErrorEnd = Code(-32000) //nolint:deadcode,varcheck
4444
)
4545

4646
// Error represents a jsonrpc2 error.

jsonrpc2.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func WithOverloaded(rejectIfOverloaded bool) Options {
115115

116116
var defaultHandler = func(ctx context.Context, conn *Conn, req *Request) {
117117
if req.IsNotify() {
118-
conn.Reply(ctx, req, nil, Errorf(CodeMethodNotFound, "method %q not found", req.Method))
118+
conn.Reply(ctx, req, nil, Errorf(MethodNotFound, "method %q not found", req.Method))
119119
}
120120
}
121121

@@ -170,7 +170,7 @@ func (c *Conn) Notify(ctx context.Context, method string, params interface{}) er
170170
c.Logger.Debug("Notify", zap.String("method", method), zap.Any("params", params))
171171
p, err := c.marshalInterface(params)
172172
if err != nil {
173-
return Errorf(CodeParseError, "failed to marshaling notify parameters: %v", err)
173+
return Errorf(ParseError, "failed to marshaling notify parameters: %v", err)
174174
}
175175

176176
req := &NotificationMessage{
@@ -180,7 +180,7 @@ func (c *Conn) Notify(ctx context.Context, method string, params interface{}) er
180180
}
181181
data, err := json.Marshal(req) // TODO(zchee): use gojay
182182
if err != nil {
183-
return Errorf(CodeParseError, "failed to marshaling notify request: %v", err)
183+
return Errorf(ParseError, "failed to marshaling notify request: %v", err)
184184
}
185185

186186
c.Logger.Debug(Send,
@@ -190,7 +190,7 @@ func (c *Conn) Notify(ctx context.Context, method string, params interface{}) er
190190

191191
err = c.stream.Write(ctx, data)
192192
if err != nil {
193-
return Errorf(CodeInternalError, "failed to write notify request data to steam: %v", err)
193+
return Errorf(InternalError, "failed to write notify request data to steam: %v", err)
194194
}
195195

196196
return nil
@@ -201,7 +201,7 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
201201
c.Logger.Debug("Call", zap.String("method", method), zap.Any("params", params))
202202
p, err := c.marshalInterface(params)
203203
if err != nil {
204-
return Errorf(CodeParseError, "failed to marshaling call parameters: %v", err)
204+
return Errorf(ParseError, "failed to marshaling call parameters: %v", err)
205205
}
206206

207207
id := ID{Number: c.seq.Add(1)}
@@ -215,7 +215,7 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
215215
// marshal the request now it is complete
216216
data, err := json.Marshal(req) // TODO(zchee): use gojay
217217
if err != nil {
218-
return Errorf(CodeParseError, "failed to marshaling call request: %v", err)
218+
return Errorf(ParseError, "failed to marshaling call request: %v", err)
219219
}
220220

221221
rchan := make(chan *Response)
@@ -237,7 +237,7 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
237237
)
238238

239239
if err := c.stream.Write(ctx, data); err != nil {
240-
return Errorf(CodeInternalError, "failed to write call request data to steam: %v", err)
240+
return Errorf(InternalError, "failed to write call request data to steam: %v", err)
241241
}
242242

243243
// wait for the response
@@ -258,7 +258,7 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
258258

259259
if err := json.Unmarshal(*resp.Result, result); err != nil {
260260
// if err := gojay.Unsafe.Unmarshal(*resp.Result, result); err != nil {
261-
return Errorf(CodeParseError, "failed to unmarshalling result: %v", err)
261+
return Errorf(ParseError, "failed to unmarshalling result: %v", err)
262262
}
263263

264264
return nil
@@ -275,7 +275,7 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
275275
func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err error) error {
276276
c.Logger.Debug("Reply")
277277
if req.IsNotify() {
278-
return NewError(CodeInvalidRequest, "reply not invoked with a valid call")
278+
return NewError(InvalidRequest, "reply not invoked with a valid call")
279279
}
280280

281281
c.handlingMu.Lock()
@@ -285,7 +285,7 @@ func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err
285285
}
286286
c.handlingMu.Unlock()
287287
if !found {
288-
return Errorf(CodeInternalError, "not a call in progress: %v", req.ID)
288+
return Errorf(InternalError, "not a call in progress: %v", req.ID)
289289
}
290290

291291
elapsed := time.Since(handling.start)
@@ -318,7 +318,7 @@ func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err
318318
zap.Any("resp.Result", resp.Result),
319319
zap.Error(err),
320320
)
321-
return Errorf(CodeParseError, "failed to marshaling reply response: %v", err)
321+
return Errorf(ParseError, "failed to marshaling reply response: %v", err)
322322
}
323323

324324
c.Logger.Debug(Send,
@@ -330,7 +330,7 @@ func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err
330330
if err := c.stream.Write(ctx, data); err != nil {
331331
// TODO(iancottrell): if a stream write fails, we really need to shut down
332332
// the whole stream
333-
return Errorf(CodeInternalError, "failed to write response data to steam: %v", err)
333+
return Errorf(InternalError, "failed to write response data to steam: %v", err)
334334
}
335335

336336
return nil
@@ -386,7 +386,7 @@ func (c *Conn) Run(ctx context.Context) (err error) {
386386
// a badly formed message arrived, log it and continue
387387
// we trust the stream to have isolated the error to just this message
388388
c.Logger.Debug(Receive,
389-
zap.Error(Errorf(CodeParseError, "unmarshal failed: %v", err)),
389+
zap.Error(Errorf(ParseError, "unmarshal failed: %v", err)),
390390
)
391391
continue
392392
}
@@ -429,7 +429,7 @@ func (c *Conn) Run(ctx context.Context) (err error) {
429429

430430
if !c.deliver(ctxReq, queuec, req) {
431431
// queue is full, reject the message by directly replying
432-
c.Reply(ctx, req, nil, Errorf(CodeServerOverloaded, "no room in queue"))
432+
c.Reply(ctx, req, nil, Errorf(ServerOverloaded, "no room in queue"))
433433
}
434434
}
435435

@@ -453,7 +453,7 @@ func (c *Conn) Run(ctx context.Context) (err error) {
453453
close(rchan) // for the range channel loop
454454

455455
default:
456-
c.Logger.Warn(Receive, zap.Error(NewError(CodeInvalidParams, "ignoring because message not a call, notify or response")))
456+
c.Logger.Warn(Receive, zap.Error(NewError(InvalidParams, "ignoring because message not a call, notify or response")))
457457
}
458458
}
459459
}

0 commit comments

Comments
 (0)