-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_error.go
55 lines (45 loc) · 1.14 KB
/
http_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
package errors
import (
"fmt"
. "github.com/cloudfoundry/cli/cf/i18n"
)
//go:generate counterfeiter . HTTPError
type HTTPError interface {
Error() string
StatusCode() int // actual HTTP status code
ErrorCode() string // error code returned in response body from CC or UAA
}
type baseHTTPError struct {
statusCode int
apiErrorCode string
description string
}
type HTTPNotFoundError struct {
baseHTTPError
}
func NewHTTPError(statusCode int, code string, description string) error {
err := baseHTTPError{
statusCode: statusCode,
apiErrorCode: code,
description: description,
}
switch statusCode {
case 404:
return &HTTPNotFoundError{err}
default:
return &err
}
}
func (err *baseHTTPError) StatusCode() int {
return err.statusCode
}
func (err *baseHTTPError) Error() string {
return fmt.Sprintf(T("Server error, status code: {{.ErrStatusCode}}, error code: {{.ErrAPIErrorCode}}, message: {{.ErrDescription}}",
map[string]interface{}{"ErrStatusCode": err.statusCode,
"ErrAPIErrorCode": err.apiErrorCode,
"ErrDescription": err.description}),
)
}
func (err *baseHTTPError) ErrorCode() string {
return err.apiErrorCode
}