-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
37 lines (32 loc) · 939 Bytes
/
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
package zia
import (
"fmt"
"io/ioutil"
"net/http"
)
type ErrorResponse struct {
Response *http.Response
Err error
Message string
}
func (r *ErrorResponse) Error() string {
if r.Response != nil {
return fmt.Sprintf("FAILED: %v, %v, %d, %v, %v, %v", r.Response.Request.Method, r.Response.Request.URL, r.Response.StatusCode, r.Response.Status, r.Message, r.Err)
}
return fmt.Sprintf("FAILED: %v", r.Err)
}
func checkErrorInResponse(res *http.Response, respErr error) error {
if c := res.StatusCode; c >= 200 && c <= 299 {
return respErr
}
errorResponse := &ErrorResponse{Response: res, Err: respErr}
errorMessage, err := ioutil.ReadAll(res.Body)
if err == nil && len(errorMessage) > 0 {
errorResponse.Message = string(errorMessage)
}
return errorResponse
}
// IsObjectNotFound returns true on missing object error (404).
func (r ErrorResponse) IsObjectNotFound() bool {
return r.Response.StatusCode == 404
}