-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherrors.go
More file actions
48 lines (43 loc) · 1015 Bytes
/
errors.go
File metadata and controls
48 lines (43 loc) · 1015 Bytes
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 httpx
import (
"fmt"
"net/http"
"github.com/imroc/req/v3"
)
type HTTPError struct {
StatusCode int
Status string
Body []byte
Header http.Header
}
// Error returns a short, human-friendly summary of the HTTP error.
// @group Errors
//
// Example: check for HTTP errors
//
// type User struct {
// Name string `json:"name"`
// }
//
// c := httpx.New()
// res, err := httpx.Get[map[string]any](c, "https://httpbin.org/status/404")
// httpx.Dump(res) // dumps map[string]any
// // map[string]interface {}(nil)
// var httpErr *httpx.HTTPError
// if errors.As(err, &httpErr) {
// _ = httpErr.StatusCode
// }
func (e *HTTPError) Error() string {
return fmt.Sprintf("httpx: http %d %s", e.StatusCode, e.Status)
}
func newHTTPError(resp *req.Response) *HTTPError {
if resp == nil {
return &HTTPError{StatusCode: 0, Status: "missing response"}
}
return &HTTPError{
StatusCode: resp.StatusCode,
Status: resp.Status,
Body: resp.Bytes(),
Header: resp.Header,
}
}