-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpcerror.go
81 lines (66 loc) · 1.77 KB
/
rpcerror.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
74
75
76
77
78
79
80
81
package gorpc
import (
"encoding/json"
"fmt"
log "github.com/Sirupsen/logrus"
)
const ErrParseError = -32700 // An error occurred on the server while parsing the JSON text.
const ErrInvalidRequest = -32600 // The JSON sent is not a valid Request object.
const ErrMethodNotFound = -32601 // The method does not exist / is not available.
const ErrInvalidParams = -32602 // Invalid method parameter(s).
const ErrInternalError = -32603 // Internal error
const ErrNotAllowed = -32000 // Not allowed custom exception
type RpcError struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func NewRpcError(code int, previous error) IRpcError {
var message string
switch code {
case ErrParseError:
message = "Parse Error"
case ErrInvalidRequest:
message = "Invalid Request"
case ErrMethodNotFound:
message = "Method Not Found"
case ErrInvalidParams:
message = "Invalid Params"
case ErrNotAllowed:
message = "Not Allowed"
default:
message = "Internal Error"
code = ErrInternalError
}
return &RpcError{Code: code, Message: message, Data: previous}
}
func (p *RpcError) GetCode() int {
return p.Code
}
func (p *RpcError) GetMessage() string {
return p.Message
}
func (p *RpcError) GetData() error {
if p.Data != nil {
return p.Data.(error)
}
return nil
}
func (p *RpcError) Error() string {
var s string
if p.Data != nil {
s = p.Data.(error).Error()
} else {
s = "nil"
}
return fmt.Sprintf("Code: %d, Message: %s, Data: %s", p.Code, p.Message, s)
}
func (this *RpcError) MarshalJSON() (result []byte, err error) {
e := *this
if e.Data != nil {
e.Data = e.Data.(error).Error()
}
result, err = json.Marshal(e)
log.Debugf("RpcError Encoded: %s, %v", string(result), err)
return result, err
}