-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
68 lines (56 loc) · 1.75 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
package hyper
import (
"context"
"net/http"
"github.com/infiniteloopcloud/go/weird"
)
func BadRequest(ctx context.Context, msg string, err error) error {
if weirdErr, ok := err.(weird.Error); ok {
weirdErr.InnerError = err
return weirdErr
}
return weird.New(msg, err, http.StatusBadRequest)
}
func ReturnBadRequest(ctx context.Context, w http.ResponseWriter, msg string, err error) {
Error(ctx, w, BadRequest(ctx, msg, err))
}
func NotFound(ctx context.Context, msg string, err error) error {
if weirdErr, ok := err.(weird.Error); ok {
weirdErr.InnerError = err
return weirdErr
}
return weird.New(msg, err, http.StatusNotFound)
}
func ReturnNotFound(ctx context.Context, w http.ResponseWriter, msg string, err error) {
Error(ctx, w, NotFound(ctx, msg, err))
}
func Unauthorized(ctx context.Context, err error) error {
if weirdErr, ok := err.(weird.Error); ok {
weirdErr.InnerError = err
return weirdErr
}
return weird.New("", err, http.StatusUnauthorized)
}
func ReturnUnauthorized(ctx context.Context, w http.ResponseWriter, err error) {
Error(ctx, w, Unauthorized(ctx, err))
}
func Forbidden(ctx context.Context, err error) error {
if weirdErr, ok := err.(weird.Error); ok {
weirdErr.InnerError = err
return weirdErr
}
return weird.New("", err, http.StatusForbidden)
}
func ReturnForbidden(ctx context.Context, w http.ResponseWriter, err error) {
Error(ctx, w, Forbidden(ctx, err))
}
func InternalServerError(ctx context.Context, err error) error {
if weirdErr, ok := err.(weird.Error); ok {
weirdErr.InnerError = err
return weirdErr
}
return weird.New("", err, http.StatusInternalServerError)
}
func ReturnInternalServerError(ctx context.Context, w http.ResponseWriter, err error) {
Error(ctx, w, InternalServerError(ctx, err))
}