-
Notifications
You must be signed in to change notification settings - Fork 10
/
error.go
73 lines (56 loc) · 2.93 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (C) 2017 Michael J. Fromberger. All Rights Reserved.
package jrpc2
import (
"encoding/json"
"errors"
"fmt"
)
// Error is the concrete type of errors returned from RPC calls.
// It also represents the JSON encoding of the JSON-RPC error object.
type Error struct {
Code Code `json:"code"` // the machine-readable error code
Message string `json:"message,omitempty"` // the human-readable error message
Data json.RawMessage `json:"data,omitempty"` // optional ancillary error data
}
// Error returns a human-readable description of e.
func (e Error) Error() string { return fmt.Sprintf("[%d] %s", e.Code, e.Message) }
// ErrCode trivially satisfies the [ErrCoder] interface.
func (e Error) ErrCode() Code { return e.Code }
// WithData marshals v as JSON and constructs a copy of e whose Data field
// includes the result. If v == nil or if marshaling v fails, e is returned
// without modification.
func (e *Error) WithData(v any) *Error {
if v == nil {
return e
} else if data, err := json.Marshal(v); err == nil {
return &Error{Code: e.Code, Message: e.Message, Data: data}
}
return e
}
// errServerStopped is returned by Server.Wait when the server was shut down by
// an explicit call to its Stop method or orderly termination of its channel.
var errServerStopped = errors.New("the server has been stopped")
// errClientStopped is the error reported when a client is shut down by an
// explicit call to its Close method.
var errClientStopped = errors.New("the client has been stopped")
// errEmptyMethod is the error reported for an empty request method name.
var errEmptyMethod = &Error{Code: InvalidRequest, Message: "empty method name"}
// errNoSuchMethod is the error reported for an unknown method name.
var errNoSuchMethod = &Error{Code: MethodNotFound, Message: MethodNotFound.String()}
// errDuplicateID is the error reported for a duplicated request ID.
var errDuplicateID = &Error{Code: InvalidRequest, Message: "duplicate request ID"}
// errInvalidRequest is the error reported for an invalid request object or batch.
var errInvalidRequest = &Error{Code: ParseError, Message: "invalid request value"}
// errEmptyBatch is the error reported for an empty request batch.
var errEmptyBatch = &Error{Code: InvalidRequest, Message: "empty request batch"}
// errInvalidParams is the error reported for invalid request parameters.
var errInvalidParams = &Error{Code: InvalidParams, Message: InvalidParams.String()}
// errTaskNotExecuted is the internal sentinel error for an unassigned task.
var errTaskNotExecuted = new(Error)
// ErrConnClosed is returned by a server's push-to-client methods if they are
// called after the client connection is closed.
var ErrConnClosed = errors.New("client connection is closed")
// Errorf returns an Error with the specified code and formatted message.
func Errorf(code Code, msg string, args ...any) *Error {
return &Error{Code: code, Message: fmt.Sprintf(msg, args...)}
}