This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
errors.go
188 lines (153 loc) · 3.37 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package rpc
import (
"encoding/json"
"fmt"
"blockwatch.cc/tzgo/micheline"
)
const (
// ErrorKindPermanent Tezos RPC error kind.
ErrorKindPermanent = "permanent"
// ErrorKindTemporary Tezos RPC error kind.
ErrorKindTemporary = "temporary"
// ErrorKindBranch Tezos RPC error kind.
ErrorKindBranch = "branch"
)
func ErrorStatus(err error) int {
switch e := err.(type) {
case *httpError:
return e.statusCode
default:
return 0
}
}
// Error is a Tezos error as documented on http://tezos.gitlab.io/mainnet/api/errors.html.
type Error interface {
error
ErrorID() string
ErrorKind() string
}
// GenericError is a basic error type
type GenericError struct {
ID string `json:"id"`
Kind string `json:"kind"`
With micheline.Prim `json:"with"`
}
func (e GenericError) Error() string {
var reason string
if e.With.IsValid() {
reason = e.With.String
}
return fmt.Sprintf("tezos: kind=%s, id=%s, reason=%s", e.Kind, e.ID, reason)
}
// ErrorID returns Tezos error id
func (e GenericError) ErrorID() string {
return e.ID
}
// ErrorKind returns Tezos error kind
func (e GenericError) ErrorKind() string {
return e.Kind
}
// HTTPStatus interface represents an unprocessed HTTP reply
type HTTPStatus interface {
Request() string // e.g. GET /...
Status() string // e.g. "200 OK"
StatusCode() int // e.g. 200
Body() []byte
}
// HTTPError retains HTTP status
type HTTPError interface {
error
HTTPStatus
}
// RPCError is a Tezos RPC error as documented on http://tezos.gitlab.io/mainnet/api/errors.html.
type RPCError interface {
Error
HTTPStatus
Errors() []Error // returns all errors as a slice
}
// Errors is a slice of Error with custom JSON unmarshaller
type Errors []Error
// UnmarshalJSON implements json.Unmarshaler
func (e *Errors) UnmarshalJSON(data []byte) error {
var errs []*GenericError
if err := json.Unmarshal(data, &errs); err != nil {
return err
}
*e = make(Errors, len(errs))
for i, g := range errs {
// TODO: handle different kinds
(*e)[i] = g
}
return nil
}
func (e Errors) Error() string {
if len(e) == 0 {
return ""
}
return e[0].Error()
}
// ErrorID returns Tezos error id
func (e Errors) ErrorID() string {
if len(e) == 0 {
return ""
}
return e[0].ErrorID()
}
// ErrorKind returns Tezos error kind
func (e Errors) ErrorKind() string {
if len(e) == 0 {
return ""
}
return e[0].ErrorKind()
}
type httpError struct {
request string
status string
statusCode int
body []byte
}
func (e *httpError) Error() string {
return fmt.Sprintf("rpc: %s status %d (%v)", e.request, e.statusCode, string(e.body))
}
func (e *httpError) Request() string {
return e.request
}
func (e *httpError) Status() string {
return e.status
}
func (e *httpError) StatusCode() int {
return e.statusCode
}
func (e *httpError) Body() []byte {
return e.body
}
type rpcError struct {
*httpError
errors Errors
}
func (e *rpcError) Error() string {
return e.errors.Error()
}
func (e *rpcError) ErrorID() string {
return e.errors.ErrorID()
}
func (e *rpcError) ErrorKind() string {
return e.errors.ErrorKind()
}
func (e *rpcError) Errors() []Error {
return e.errors
}
type plainError struct {
*httpError
msg string
}
func (e *plainError) Error() string {
return e.msg
}
var (
_ Error = &GenericError{}
_ Error = Errors{}
_ RPCError = &rpcError{}
)