aboutsummaryrefslogtreecommitdiffstats
path: root/err.go
blob: 30c7f8f4e255c9b1caf8940631a0b2fb57aa8604 (plain) (blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package lulu

import (
	"fmt"
	"io"
	"net/http"
)

// pkgErr formats an error to be returned to the package user.
func pkgErr(err error) error {
	return fmt.Errorf("lulu: %w", err)
}

// pkgErrf formats an error with a message to be returned to the package user.
func pkgErrf(err error, format string, a ...any) error {
	return fmt.Errorf("lulu: %s: %w",
		fmt.Sprintf(format, a...),
		err)
}

// error encoding request
type errEncReq struct {
	payload any
	path    string
	error
}

func (e errEncReq) Error() string {
	return fmt.Sprintf("error encoding request body %v for %s: %v", e.payload, e.path, e.error)
}

// server responded with the wrong status
type errRespStatus struct {
	*http.Response
}

func (e errRespStatus) Error() string {
	resp := e.Response
	req := resp.Request
	body, _ := io.ReadAll(resp.Body)
	return fmt.Sprintf("%s %s: %s: %s", req.Method, req.URL, resp.Status, body)
}

// error reading response
type errReadResp struct {
	*http.Response
	error
}

func (e errReadResp) Error() string {
	req := e.Response.Request
	return fmt.Sprintf("%s %s: error reading response body: %v",
		req.Method, req.URL, e.error)
}

// error decoding response
type errDecResp struct {
	*http.Response
	body []byte
	error
}

func (e errDecResp) Error() string {
	req := e.Response.Request
	return fmt.Sprintf("%s %s: error decoding response body `%s`: %v",
		req.Method, req.URL, string(e.body), e.error)
}

// server returned a bad response
type errResp struct {
	*http.Response
	error
}

func (e errResp) Error() string {
	req := e.Response.Request
	return fmt.Sprintf("%s %s: bad response: %v", req.Method, req.URL, e.error)
}