aboutsummaryrefslogtreecommitdiff
path: root/rpc
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-05-22 03:27:50 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-05-22 03:27:50 +0300
commitc74596c6a6a741e3365a2f372de6e7cdf2583fdc (patch)
tree47325deb81a08c80efd405144a8e7eecdee45f6d /rpc
parent262768e3a2298c9ae51bab238b55e2d5483233f6 (diff)
Better errorsv1.0.2
Diffstat (limited to 'rpc')
-rw-r--r--rpc/errors.go12
-rw-r--r--rpc/server.go4
-rw-r--r--rpc/wrapper.go2
3 files changed, 14 insertions, 4 deletions
diff --git a/rpc/errors.go b/rpc/errors.go
index a18c712..71a7168 100644
--- a/rpc/errors.go
+++ b/rpc/errors.go
@@ -50,7 +50,7 @@ func (e Error) Error() string {
return fmt.Sprintf("jsonrpc2 error: code: %d message: %s", e.Code, e.Message)
}
-func NewError(code int) Error {
+func ErrorFromCode(code int) Error {
if _, ok := errorMap[code]; ok {
return Error{
Code: code,
@@ -59,3 +59,13 @@ func NewError(code int) Error {
}
return Error{Code: code}
}
+
+func NewError(message string, code int) Error {
+ if code == 0 {
+ code = ErrUser
+ }
+ return Error{
+ Code: code,
+ Message: message,
+ }
+}
diff --git a/rpc/server.go b/rpc/server.go
index 1bb15d5..9c5e847 100644
--- a/rpc/server.go
+++ b/rpc/server.go
@@ -114,7 +114,7 @@ func (r *RpcServer) callMethod(ctx context.Context, req *rpcRequest) *rpcRespons
if !ok {
return &rpcResponse{
Jsonrpc: version,
- Error: NewError(ErrCodeMethodNotFound),
+ Error: ErrorFromCode(ErrCodeMethodNotFound),
Id: req.Id,
}
}
@@ -137,7 +137,7 @@ func (r *RpcServer) callMethod(ctx context.Context, req *rpcRequest) *rpcRespons
func WriteError(code int, enc *json.Encoder) {
enc.Encode(rpcResponse{
Jsonrpc: version,
- Error: NewError(code),
+ Error: ErrorFromCode(code),
})
}
diff --git a/rpc/wrapper.go b/rpc/wrapper.go
index 9b4de28..1d6361c 100644
--- a/rpc/wrapper.go
+++ b/rpc/wrapper.go
@@ -28,7 +28,7 @@ func H[RQ any, RS any](handler func(context.Context, *RQ) (RS, error)) Handler {
return func(ctx context.Context, in json.RawMessage) (json.RawMessage, error) {
req := new(RQ)
if err := json.Unmarshal(in, req); err != nil {
- return nil, NewError(ErrCodeParseError)
+ return nil, ErrorFromCode(ErrCodeParseError)
}
resp, err := handler(ctx, req)
if err != nil {