forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_error.go
53 lines (44 loc) · 1.09 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
package errors
import (
"fmt"
. "github.com/cloudfoundry/cli/cf/i18n"
)
type HttpError interface {
error
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
}