-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
48 lines (41 loc) · 1.06 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
package snet
import (
"errors"
"fmt"
"io"
"net/http"
)
// //////////////////////////////////////////////////////////////
// WrongStatusError
var _ error = WrongStatusError{}
type WrongStatusError struct {
ResponseBody []byte `json:"response_body"`
ResponseStatusCode int `json:"response_status_code"`
RequestURL string `json:"request_url"`
RequestMethod string `json:"request_method"`
}
func (err WrongStatusError) Error() string {
return fmt.Sprintf(
"wrong status code for [%s %s]; Code: [%d] with reponse [%s] ",
err.RequestMethod, err.RequestURL,
err.ResponseStatusCode, err.ResponseBody,
)
}
func NewWrongStatusError(r *http.Response) error {
defer func() { _ = r.Body.Close() }()
b, _ := io.ReadAll(r.Body)
return WrongStatusError{
ResponseBody: b,
ResponseStatusCode: r.StatusCode,
RequestURL: r.Request.URL.String(),
RequestMethod: r.Request.Method,
}
}
func IsWrongStatusError(err error) *WrongStatusError {
var e WrongStatusError
if errors.As(err, &e) {
return &e
} else {
return nil
}
}