/
error.go
143 lines (121 loc) · 2.99 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
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
package pangea
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type PangeaErrors struct {
Errors []ErrorField `json:"errors,omitempty"`
}
type ErrorField struct {
Code string `json:"code"`
Detail string `json:"detail"`
Source string `json:"source"`
Path string `json:"path,omitempty"`
}
type BaseError struct {
// The underlying error that triggered this one, if any.
Err error
// the HTTP response
HTTPResponse *http.Response
}
type APIError struct {
BaseError
// the reponse header of the request if any
ResponseHeader *ResponseHeader
// the result of the request
RawResult json.RawMessage
// error details
PangeaErrors PangeaErrors
}
func NewAPIError(err error, r *Response) *APIError {
var errRes error
var pa PangeaErrors
errUnm := r.UnmarshalResult(&pa)
if errUnm != nil {
pa = PangeaErrors{}
errRes = fmt.Errorf("Error: %s. Unmarshall Error: %s.", err.Error(), errUnm.Error())
} else {
errRes = fmt.Errorf("Error: %s.", err.Error())
}
return &APIError{
BaseError: BaseError{
Err: errRes,
HTTPResponse: r.HTTPResponse,
},
RawResult: r.RawResult,
ResponseHeader: &r.ResponseHeader,
PangeaErrors: pa,
}
}
func (e *BaseError) Error() string {
return e.Err.Error()
}
func (e *APIError) Error() string {
b := new(bytes.Buffer)
if e.Err != nil {
b.WriteString(fmt.Sprintf("%s\n", e.Err.Error()))
}
if e.HTTPResponse != nil {
b.WriteString(fmt.Sprintf("%v %v\n", e.HTTPResponse.Request.Method, e.HTTPResponse.Request.URL))
if e.ResponseHeader != nil {
b.WriteString(StringifyIndented(e.ResponseHeader))
} else {
b.WriteString(fmt.Sprintf("Status code: %v\n", e.HTTPResponse.StatusCode))
}
}
if len(e.PangeaErrors.Errors) > 0 {
b.WriteString("Errors:\n")
b.WriteString(StringifyIndented(e.PangeaErrors.Errors))
}
return b.String()
}
type UnmarshalError struct {
BaseError
Bytes []byte
}
func NewUnmarshalError(err error, bytes []byte, r *http.Response) *UnmarshalError {
return &UnmarshalError{
BaseError: BaseError{
Err: err,
HTTPResponse: r,
},
Bytes: bytes,
}
}
func (e *UnmarshalError) Error() string {
b := new(bytes.Buffer)
b.WriteString("Failed to unmarshall body")
if len(e.Bytes) > 0 {
pad(b, ": ")
b.WriteString(fmt.Sprintf("body: %s", string(e.Bytes)))
}
return b.String()
}
// pad appends str to the buffer if the buffer already has some data.
func pad(b *bytes.Buffer, str string) {
if b.Len() == 0 {
return
}
b.WriteString(str)
}
type AcceptedError struct {
ResponseHeader
ResultField any
}
func (e *AcceptedError) Error() string {
return fmt.Sprintf("request scheduled on Pangea side: please check the status of the request %v later", e.ReqID())
}
// Is returns whether the provided error equals this error.
func (e *AcceptedError) Is(target error) bool {
v, ok := target.(*AcceptedError)
if !ok {
return false
}
return v.RequestID == e.RequestID
}
// ReqID is a helper function to get the request ID
func (e *AcceptedError) ReqID() string {
return StringValue(e.RequestID)
}