-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
66 lines (57 loc) · 1.71 KB
/
Copy patherror.go
File metadata and controls
66 lines (57 loc) · 1.71 KB
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
package jsonrpc
import (
"context"
"encoding/json"
"fmt"
"github.com/gumeniukcom/golang-jsonrpc2/v2/structs"
)
// ErrorMessages maps error codes to human-readable messages.
type ErrorMessages map[int]string
// RegisterError registers a custom error code and message.
// Codes in the range -32768..-32000 are reserved by the JSON-RPC spec.
// See http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
func (j *JSONRPC) RegisterError(code int, msg string) error {
j.mu.Lock()
defer j.mu.Unlock()
if _, ok := j.errors[code]; ok {
return fmt.Errorf("error with code %d already exists", 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 builds a JSON-RPC error response for the given error code and request ID.
func (j *JSONRPC) Error(
ctx context.Context,
err error,
errorCode int,
id any,
) *structs.Response {
j.mu.RLock()
errMsg, ok := j.errors[errorCode]
internalMsg := j.errors[InternalErrorCode]
j.mu.RUnlock()
if err == nil {
err = fmt.Errorf("")
}
if !ok {
return newError(internalMsg, InternalErrorCode, err.Error(), id)
}
return newError(errMsg, errorCode, err.Error(), id)
}
func newError(errMsg string, errorCode int, info string, id any) *structs.Response {
return NewResponse(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":null}`)
}
func errorInvalidRequest() json.RawMessage {
return []byte(
`{"jsonrpc":"2.0","error":{"code":-32600,"message":"invalid_request_not_conforming_to_spec"},"id":null}`)
}