-
Notifications
You must be signed in to change notification settings - Fork 0
/
Error.go
63 lines (53 loc) · 1.25 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
package libremotebuild
import (
"errors"
"fmt"
)
var (
// ErrInvalidResponseHeaders error on missing or malformed response headers
ErrInvalidResponseHeaders = errors.New("Invalid response headers")
// ErrResponseError response returned an error
ErrResponseError = errors.New("response returned an error")
)
// ResponseErr response error
type ResponseErr struct {
Response *RestRequestResponse
Err error
}
func (reserr *ResponseErr) Error() string {
if reserr.Response != nil {
return fmt.Sprintf("HTTPCode: %d; Message: %s", reserr.Response.HTTPCode, reserr.Response.Message)
}
if reserr.Err != nil {
return reserr.Err.Error()
}
return "Unexpected error"
}
// NewErrorFromResponse return error from response
func NewErrorFromResponse(r *RestRequestResponse, err ...error) *ResponseErr {
var (
responseErr ResponseErr
e error
)
// use e if err passed
if len(err) > 0 && err[0] != nil {
e = err[0]
}
// Check if http.Request was successful
if r != nil {
// Server throw an error
if r.Status == ResponseError && e == nil {
e = ErrResponseError
}
responseErr = ResponseErr{
Response: r,
Err: e,
}
} else {
// http.Request throw an error
responseErr = ResponseErr{
Err: e,
}
}
return &responseErr
}