-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.go
58 lines (49 loc) · 1.07 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
package swift
import (
"net/http"
)
func NotFound(err error) bool {
httpError, ok := err.(*ErrWithHTTPResponse)
if !ok {
return false
}
if httpError.NotFound() {
return true
}
return false
}
func UnAuthorized(err error) bool {
httpError, ok := err.(*ErrWithHTTPResponse)
if !ok {
return false
}
if httpError.UnAuthorized() {
return true
}
return false
}
type ErrWithHTTPResponse struct {
HTTPResponse *http.Response
Err error
}
func NewErrWithHTTPResponse(HTTPResponse *http.Response, err error) error {
if err == nil {
return nil
}
if HTTPResponse == nil {
return err
}
return &ErrWithHTTPResponse{HTTPResponse: HTTPResponse, Err: err}
}
func (e *ErrWithHTTPResponse) UnAuthorized() bool {
return e.HTTPResponse.StatusCode == http.StatusUnauthorized
}
func (e *ErrWithHTTPResponse) NotFound() bool {
return e.HTTPResponse.StatusCode == http.StatusNotFound
}
func (e *ErrWithHTTPResponse) Unwrap() error {
return e.Err
}
func (e *ErrWithHTTPResponse) Error() string {
return "http status code " + e.HTTPResponse.Status + ": " + e.Err.Error()
}