-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcustom_response.go
108 lines (90 loc) · 2.66 KB
/
custom_response.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
99
100
101
102
103
104
105
106
107
108
package api
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/GitDataAI/jiaozifs/auth"
"github.com/GitDataAI/jiaozifs/models"
)
type JiaozifsResponse struct {
http.ResponseWriter
}
// JSON convert object to json format and write to response,
// if not specific code, default code is 200. given code will
// overwrite default code, if more than one code, the first one will be used.
func (response *JiaozifsResponse) JSON(v any, code ...int) {
response.Header().Set("Content-Type", "application/json")
if len(code) == 0 {
response.WriteHeader(http.StatusOK)
} else {
response.WriteHeader(code[0])
}
err := json.NewEncoder(response.ResponseWriter).Encode(v)
if err != nil {
response.Error(err)
return
}
}
// OK response with 200
func (response *JiaozifsResponse) OK() {
response.WriteHeader(http.StatusOK)
}
// NotFound response with 404
func (response *JiaozifsResponse) NotFound() {
response.WriteHeader(http.StatusNotFound)
}
func (response *JiaozifsResponse) Forbidden() {
response.WriteHeader(http.StatusForbidden)
}
// Unauthorized response with 401
func (response *JiaozifsResponse) Unauthorized() {
response.WriteHeader(http.StatusUnauthorized)
}
func (response *JiaozifsResponse) BadRequest(msg string, args ...any) {
response.WriteHeader(http.StatusBadRequest)
_, _ = response.Write([]byte(fmt.Sprintf(msg, args...)))
}
// Error response with 500 and error message
func (response *JiaozifsResponse) Error(err error) {
if errors.Is(err, models.ErrNotFound) {
response.WriteHeader(http.StatusNotFound)
_, _ = response.Write([]byte(err.Error()))
return
}
if errors.Is(err, auth.ErrUserNotFound) {
response.WriteHeader(http.StatusUnauthorized)
return
}
var codeErr ErrCode
if errors.As(err, &codeErr) {
response.WriteHeader(int(codeErr))
_, _ = response.Write([]byte(err.Error()))
return
}
response.WriteHeader(http.StatusInternalServerError)
_, _ = response.Write([]byte(err.Error()))
}
// String response and string
// if not specific code, default code is 200. given code will
// overwrite default code, if more than one code, the first one will be used.
func (response *JiaozifsResponse) String(msg string, code ...int) {
response.Header().Set("Content-Type", "text/plain;charset=UTF-8")
if len(code) == 0 {
response.WriteHeader(http.StatusOK)
} else {
response.WriteHeader(code[0])
}
_, _ = response.Write([]byte(msg))
}
// Code response with uncommon code
func (response *JiaozifsResponse) Code(code int) {
response.WriteHeader(code)
}
type ErrCode int
func NewErrCode(code int) ErrCode {
return ErrCode(code)
}
func (err ErrCode) Error() string {
return fmt.Sprintf("code %d msg %s", err, http.StatusText(int(err)))
}