-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherror.go
41 lines (33 loc) · 935 Bytes
/
error.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
package httpwrap
import (
"fmt"
"io"
)
type HTTPError interface {
error
HTTPResponse
}
// httpError implements both the HTTPResponse interface and the standard error
// interface.
type httpError struct {
code int
body string
}
func NewHTTPError(code int, format string, args ...any) HTTPError {
return httpError{
code: code,
body: fmt.Sprintf(format, args...),
}
}
func (err httpError) Error() string {
return fmt.Sprintf("http error: %d: %s", err.code, err.body)
}
func (err httpError) StatusCode() int { return err.code }
func (err httpError) WriteBody(writer io.Writer) error {
_, writeError := io.WriteString(writer, err.body)
return writeError
}
// NewNoopError returns an HTTPError that will completely bypass the
// deserialization logic. This can be used when the endpoint or middleware
// operates directly on the native http.ResponseWriter.
func NewNoopError() HTTPError { return NewHTTPError(0, "") }