-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
69 lines (58 loc) · 1.8 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
59
60
61
62
63
64
65
66
67
68
69
package akumu
import (
"errors"
"fmt"
"net/http"
)
// ErrServer is the base error that akumu
// will return whenever there's any error
// in the >=500 - <600 range.
type ErrServer struct {
// Code determines the response's status code.
// This is used to understand what response
// was sent due to that particular error.
Code int
// Request stores the actual http request that
// failed to execute. This is very useful to
// extract information such as URL or headers
// to understand more about what caused this error.
Request *http.Request
}
var (
// ErrServerWriter determines that the
// server error comes from executing logic
// around [Builder.BodyWriter] or derivates
// that use this method.
ErrServerWriter = errors.New("builder is a body writer")
// ErrServerBody determines that the
// server error comes from executing logic
// around [Builder.BodyReader] or derivates
// that use this method.
ErrServerBody = errors.New("builder is a body reader")
// ErrServerStream determines that the
// server error comes from executing logic
// around [Builder.Stream] or derivates
// that use this method.
ErrServerStream = errors.New("builder is a body stream")
// ErrServerDefault determines that the
// server error comes from executing logic
// around having no body, stream nor writer
// involved in delivering a response.
ErrServerDefault = errors.New("builder is a default no body")
)
// Error implements the error interface
// for a server error.
func (err ErrServer) Error() string {
return fmt.Sprintf(
"%d: %s",
err.Code,
http.StatusText(err.Code),
)
}
// Is determines if the given target error
// is an [ErrServer]. This is used when dealing
// with errors using [errors.Is] and [errors.As].
func (err ErrServer) Is(target error) bool {
_, ok := target.(ErrServer)
return ok
}