/
errors.go
188 lines (163 loc) · 4.99 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
package http
import (
"errors"
"fmt"
"net/http"
"strings"
"gopkg.in/errgo.v1"
"github.com/Scalingo/go-scalingo/v7/debug"
errorspkg "github.com/Scalingo/go-utils/errors/v2"
)
type (
BadRequestError struct {
ErrMessage string `json:"error"`
Code string `json:"code"`
}
PaymentRequiredError struct {
Name string `json:"name"`
ErrMessage string `json:"error"`
URL string `json:"url"`
}
NotFoundError struct {
Resource string `json:"resource"`
Err string `json:"error"`
}
ForbiddenError struct {
Err string `json:"error"`
Code string `json:"code"`
}
UnprocessableEntity struct {
Errors map[string][]string `json:"errors"`
}
TooManyRequestsError struct {
ErrMessage string `json:"error"`
Code string `json:"code"`
}
APIError struct {
Error string `json:"error"`
}
RequestFailedError struct {
Code int
APIError error
Req *APIRequest
Message string
}
)
var ErrOTPRequired = errors.New("OTP Required")
// IsOTPRequired tests if the authentication backend return an OTP Required error
func IsOTPRequired(err error) bool {
if errorspkg.Is(err, ErrOTPRequired) {
return true
}
rerr, ok := err.(*RequestFailedError)
if !ok {
return false
}
if rerr.Message == "OTP Required" {
return true
}
return false
}
func (err BadRequestError) Error() string {
return fmt.Sprintf("400 Bad Request → %v", err.ErrMessage)
}
func (err PaymentRequiredError) Error() string {
return fmt.Sprintf("%v\n→ %v", err.ErrMessage, err.URL)
}
func (err NotFoundError) Error() string {
if err.Resource == "app" {
return "The application was not found, did you make a typo?"
} else if err.Resource == "container_type" {
return "This type of container was not found, please ensure it is present in your Procfile\n→ https://doc.scalingo.com/platform/app/procfile"
} else if err.Resource != "" {
return fmt.Sprintf("The %s was not found", err.Resource)
} else {
// Sometimes the API does not return a resource in the body, but the error
// message is self-explained.
return err.Err
}
}
func (err UnprocessableEntity) Error() string {
errArray := make([]string, 0, len(err.Errors))
for attr, attrErrs := range err.Errors {
errArray = append(errArray, fmt.Sprintf("* %s → %s", attr, strings.Join(attrErrs, ", ")))
}
return strings.Join(errArray, "\n")
}
func (err TooManyRequestsError) Error() string {
return fmt.Sprintf("429 Too Many Requests → %v", err.ErrMessage)
}
func (err ForbiddenError) Error() string {
return fmt.Sprintf("Request forbidden (403): %v", err.Err)
}
func NewRequestFailedError(res *http.Response, req *APIRequest) error {
debug.Printf("APIRequest Error: [%d] %s %s%s", res.StatusCode, req.Method, req.URL, req.Endpoint)
defer res.Body.Close()
switch res.StatusCode {
case 400:
var badRequestError BadRequestError
err := parseJSON(res, &badRequestError)
if err != nil {
return err
}
return &RequestFailedError{Code: res.StatusCode, APIError: badRequestError, Req: req}
case 401:
var apiErr APIError
err := parseJSON(res, &apiErr)
if err != nil {
return err
}
return &RequestFailedError{Code: res.StatusCode, APIError: errgo.New("unauthorized - you are not authorized to do this operation"), Req: req, Message: apiErr.Error}
case 402:
var paymentRequiredErr PaymentRequiredError
err := parseJSON(res, &paymentRequiredErr)
if err != nil {
return err
}
return &RequestFailedError{Code: res.StatusCode, APIError: paymentRequiredErr, Req: req}
case 403:
var forbiddenError ForbiddenError
err := parseJSON(res, &forbiddenError)
if err != nil {
return err
}
return &RequestFailedError{Code: res.StatusCode, APIError: forbiddenError, Req: req}
case 404:
var notFoundErr NotFoundError
err := parseJSON(res, ¬FoundErr)
if err != nil {
return err
}
return &RequestFailedError{Code: res.StatusCode, APIError: notFoundErr, Req: req}
case 422:
var unprocessableError UnprocessableEntity
err := parseJSON(res, &unprocessableError)
if err != nil {
return err
}
return &RequestFailedError{Code: res.StatusCode, APIError: unprocessableError, Req: req}
case 429:
var tooManyRequestsError TooManyRequestsError
err := parseJSON(res, &tooManyRequestsError)
if err != nil {
return err
}
return &RequestFailedError{Code: res.StatusCode, APIError: tooManyRequestsError, Req: req}
case 500:
return &RequestFailedError{Code: res.StatusCode, APIError: errgo.New("server internal error - our team has been notified"), Req: req}
case 503:
return &RequestFailedError{Code: res.StatusCode, APIError: fmt.Errorf("upstream provider returned an error, please retry later"), Req: req}
default:
return &RequestFailedError{Code: res.StatusCode, APIError: fmt.Errorf("invalid status from server: %v", res.Status), Req: req}
}
}
func (err *RequestFailedError) Error() string {
return err.APIError.Error()
}
func (err *RequestFailedError) String() string {
return err.APIError.Error()
}
func IsRequestFailedError(err error) bool {
_, ok := err.(*RequestFailedError)
return ok
}