-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
59 lines (51 loc) · 1.53 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
package jsonrpc
import (
"context"
"encoding/json"
"fmt"
"github.com/gumeniukcom/golang-jsonrpc2/structs"
)
// ErrorMessages map of errors
type ErrorMessages map[int]string
//RegisterError register new error
// @see http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
func (j *JSONRPC) RegisterError(code int, msg string) error {
if _, ok := j.errors[code]; ok {
return fmt.Errorf("error with code %d exist", code)
}
if code >= -32768 && code <= -32000 {
return fmt.Errorf("error with code %d : range -32768..-32000 reserved", code)
}
j.errors[code] = msg
return nil
}
// Error is method for create response with error code
func (j *JSONRPC) Error(
ctx context.Context,
err error,
errorCode int,
id interface{},
) *structs.Response {
errMsg, ok := j.errors[errorCode]
if err == nil {
err = fmt.Errorf("")
}
if !ok {
return newError(ctx, j.errors[InternalErrorCode], InternalErrorCode, err.Error(), id)
}
return newError(ctx, errMsg, errorCode, err.Error(), id)
}
func newError(ctx context.Context, errMsg string, errorCode int, info string, id interface{}) *structs.Response {
return Response(ctx, id, nil, &structs.Error{
Code: errorCode,
Message: errMsg,
Data: info,
})
}
func errorInternal() json.RawMessage {
return []byte("{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32603,\"message\":\"internal_error\"}, \"id\":1}")
}
func errorInvalidRequest() json.RawMessage {
return []byte(
"{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32600,\"message\":\"invalid_request_not_conforming_to_spec\"}, \"id\":1}")
}