-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
51 lines (42 loc) · 1.13 KB
/
http.go
File metadata and controls
51 lines (42 loc) · 1.13 KB
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
package errors
import (
"fmt"
"strings"
"github.com/valyala/fasthttp"
)
type HttpError interface {
StatusCode() int
}
// HttpError implements the aggregator to set the statusCode to an ErrorResponse.
type httpError struct {
reason error
statusCode int
}
// Error returns the error message of the original error.
func (err *httpError) Error() string {
message := strings.ToLower(fasthttp.StatusMessage(err.statusCode))
if err.reason == nil {
return message
}
return fmt.Sprintf("%s: %s", message, err.reason.Error())
}
// StatusCode returns the statusCode of the error.
func (err *httpError) StatusCode() int {
return err.statusCode
}
// Unwrap returns the next error in the error chain.
// If there is no next error, Unwrap returns nil.
func (err *httpError) Unwrap() error {
return err.reason
}
// AppendData aggregates the statusCode to the ErrorResponse.
func (err *httpError) AppendData(response ErrorResponse) {
response.SetParam("statusCode", err.StatusCode())
}
// WrapHttp wraps a reason with an HttpError.
func WrapHttp(reason error, status int) error {
return &httpError{
reason: reason,
statusCode: status,
}
}