-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy patherrors.go
76 lines (60 loc) · 1.3 KB
/
errors.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
package butlerd
import (
"fmt"
"github.com/sourcegraph/jsonrpc2"
)
type Error interface {
error
RpcErrorCode() int64
RpcErrorMessage() string
RpcErrorData() map[string]interface{}
}
type RpcError struct {
Code int64
Message string
}
var _ Error = (*RpcError)(nil)
func StandardRpcError(Code int64) Error {
var message string = "Unknown error"
switch Code {
case jsonrpc2.CodeParseError:
message = "Parse error"
case jsonrpc2.CodeInvalidRequest:
message = "Invalid request"
case jsonrpc2.CodeMethodNotFound:
message = "Method not found"
case jsonrpc2.CodeInvalidParams:
message = "Invalid params"
case jsonrpc2.CodeInternalError:
message = "Internal error"
}
return &RpcError{Code: Code, Message: message}
}
func (re *RpcError) RpcErrorCode() int64 {
return re.Code
}
func (re *RpcError) RpcErrorMessage() string {
return re.Message
}
func (re *RpcError) RpcErrorData() map[string]interface{} {
return nil
}
func (re *RpcError) Error() string {
return fmt.Sprintf("RPC error %d: %s", re.Code, re.Message)
}
//
type causer interface {
Cause() error
}
func AsButlerdError(err error) (Error, bool) {
if err == nil {
return nil, false
}
if se, ok := err.(causer); ok {
return AsButlerdError(se.Cause())
}
if ee, ok := err.(Error); ok {
return ee, true
}
return nil, false
}