forked from Scalingo/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
116 lines (101 loc) · 3.17 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
package api
import (
"fmt"
"net/http"
"strings"
"github.com/Scalingo/cli/Godeps/_workspace/src/gopkg.in/errgo.v1"
"github.com/Scalingo/cli/debug"
)
type (
BadRequestError struct {
ErrMessage string `json:"error"`
}
PaymentRequiredError struct {
ErrMessage string `json:"error"`
URL string `json:"url"`
}
NotFoundError struct {
Resource string `json:"resource"`
Err string `json:"error"`
}
UnprocessableEntity struct {
Errors map[string][]string `json:"errors"`
}
RequestFailedError struct {
Code int
APIError error
Req *APIRequest
}
)
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 fmt.Sprintf("The application has not been found, have you done a typo?")
} else if err.Resource == "container_type" {
return fmt.Sprintf("This type of container has not been found, please ensure it is present in your Procfile\n→ http://doc.scalingo.com/internals/procfile")
} else {
return fmt.Sprintf("The %s has not been found", err.Resource)
}
}
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 NewRequestFailedError(res *http.Response, req *APIRequest) error {
debug.Println("APIRequest Error:", res.StatusCode, req.Method, req.Endpoint)
defer res.Body.Close()
switch res.StatusCode {
case 400:
var badRequestError BadRequestError
err := ParseJSON(res, &badRequestError)
if err != nil {
return errgo.Mask(err, errgo.Any)
}
return &RequestFailedError{res.StatusCode, badRequestError, req}
case 401:
return &RequestFailedError{res.StatusCode, errgo.New("unauthorized - you are not authorized to do this operation"), req}
case 402:
var paymentRequiredErr PaymentRequiredError
err := ParseJSON(res, &paymentRequiredErr)
if err != nil {
return errgo.Mask(err, errgo.Any)
}
return &RequestFailedError{res.StatusCode, paymentRequiredErr, req}
case 404:
var notFoundErr NotFoundError
err := ParseJSON(res, ¬FoundErr)
if err != nil {
return errgo.Mask(err, errgo.Any)
}
return &RequestFailedError{res.StatusCode, notFoundErr, req}
case 422:
var unprocessableError UnprocessableEntity
err := ParseJSON(res, &unprocessableError)
if err != nil {
return errgo.Mask(err, errgo.Any)
}
return &RequestFailedError{res.StatusCode, unprocessableError, req}
case 500:
return &RequestFailedError{res.StatusCode, errgo.New("server internal error - our team has been notified"), req}
default:
return &RequestFailedError{res.StatusCode, fmt.Errorf("invalid status from server: %v", res.Status), 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
}