-
Notifications
You must be signed in to change notification settings - Fork 84
/
response.go
80 lines (60 loc) · 1.79 KB
/
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
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/go-chi/render"
)
// Response ...
type Response struct {
Status string ` json:"status"`
Message string ` json:"message"`
Data map[string]interface{} ` json:"data"`
}
// NewSuccess creates a blag successful response
// NewSuccessWithData creates a blag successful response with data
// NewFail creates a blag response with status = fail
// NewFailWithMessage creates a blag response with status = fail and a message
// NewBad ...
// NewInvalid ...
// NewError creates a blag respone with status = error
// NewResponse ...
// AddMessage adds a message to the response
func (r *Response) AddMessage(message string) {
r.Message = message
}
// AddData adds data to the response
func (r *Response) AddData(key string, data interface{}) {
r.Data[key] = data
}
// JSON generates a JSON formatted response
func (r *Response) JSON() []byte {
res, err := json.Marshal(r)
if err != nil {
log.Fatalln(err)
}
return res
}
// ErrInvalidRequest ...
func ErrInvalidRequest(err error) render.Renderer {
return &ErrResponse{
Err: err,
HTTPStatusCode: 400,
StatusText: "",
ErrorText: err.Error(),
}
}
// ErrInternal ...
// ErrResponse ...
type ErrResponse struct {
Err error `json:"-"` // low-level runtime error
HTTPStatusCode int `json:"-"` // http response status code
StatusText string `json:"status,omitempty"` // user-level status message
AppCode int64 `json:"code,omitempty"` // application-specific error code
ErrorText string `json:"message,omitempty"` // application-level error message, for debugging
}
// Render ...
func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error {
render.Status(r, e.HTTPStatusCode)
return nil
}