-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
98 lines (79 loc) · 2.61 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package zhttp
import (
"fmt"
"net/http"
)
// Error signature hold the error, which return http statusCode and body
type Error interface {
HTTPError() (int, string)
}
type httpError struct {
error
statusCode int
}
// HTTPError get status code and http body for an error
func (e *httpError) HTTPError() (int, string) {
return e.statusCode, e.Error()
}
func errorWithStatus(statusCode int, err error) error {
return &httpError{
error: err,
statusCode: statusCode,
}
}
// BadRequest return 400 with error
func BadRequest(err error) error {
return errorWithStatus(http.StatusBadRequest, err)
}
// BadRequestf return 400 with format error
func BadRequestf(format string, args ...any) error {
return errorWithStatus(http.StatusBadRequest, fmt.Errorf(format, args...))
}
// Unauthorized return 401 with error
func Unauthorized(err error) error {
return errorWithStatus(http.StatusUnauthorized, err)
}
// Unauthorizedf return 401 with format error
func Unauthorizedf(format string, args ...any) error {
return errorWithStatus(http.StatusUnauthorized, fmt.Errorf(format, args...))
}
// PaymentRequired return 402 with error
func PaymentRequired(err error) error {
return errorWithStatus(http.StatusPaymentRequired, err)
}
// PaymentRequired return 402 with format error
func PaymentRequiredf(format string, args ...any) error {
return errorWithStatus(http.StatusUnauthorized, fmt.Errorf(format, args...))
}
// Forbidden return 403 with error
func Forbidden(err error) error {
return errorWithStatus(http.StatusForbidden, err)
}
// Forbiddenf return 403 with format error
func Forbiddenf(format string, args ...any) error {
return errorWithStatus(http.StatusForbidden, fmt.Errorf(format, args...))
}
// NotFound return 404 with error
func NotFound(err error) error {
return errorWithStatus(http.StatusNotFound, err)
}
// NotFoundf return 404 with format error
func NotFoundf(format string, args ...any) error {
return errorWithStatus(http.StatusNotFound, fmt.Errorf(format, args...))
}
// NotFound return 406 with error
func NotAcceptable(err error) error {
return errorWithStatus(http.StatusNotAcceptable, err)
}
// NotAcceptablef return 406 with format error
func NotAcceptablef(format string, args ...any) error {
return errorWithStatus(http.StatusNotFound, fmt.Errorf(format, args...))
}
// InternalServerError return 500 with error
func InternalServerError(err error) error {
return errorWithStatus(http.StatusInternalServerError, err)
}
// NotAcceptablef return 500 with format error
func InternalServerErrorf(format string, args ...any) error {
return errorWithStatus(http.StatusInternalServerError, fmt.Errorf(format, args...))
}