-
Notifications
You must be signed in to change notification settings - Fork 2
/
response.go
66 lines (57 loc) · 1.39 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
package fdk
import (
"encoding/json"
"fmt"
"net/http"
)
// Response is the domain type for the response.
type Response struct {
Body json.Marshaler
Code int
Errors []APIError
Header http.Header
}
// StatusCode returns the response status code. When a Response.Code is not
// set and errors exist, then the highest code on the errors is returned.
func (r Response) StatusCode() int {
code := r.Code
if code == 0 && len(r.Errors) > 0 {
for _, e := range r.Errors {
if e.Code > code {
code = e.Code
}
}
}
if code == 0 {
code = http.StatusOK
}
return code
}
// APIError defines a error that is shared back to the caller.
type APIError struct {
Code int `json:"code"`
Message string `json:"message"`
}
// Error provides a human readable error message.
func (a APIError) Error() string {
return fmt.Sprintf("[%d] %s", a.Code, a.Message)
}
// JSON jsonifies the input to valid json upon request marshaling.
func JSON(v any) json.Marshaler {
return jsoned{v: v}
}
type jsoned struct {
v any
}
func (j jsoned) MarshalJSON() ([]byte, error) {
return json.Marshal(j.v)
}
// ErrResp creates a sad path errors only response.
//
// Note: the highest status code from the errors will be used for the response
// status if no status code is set on the response.
func ErrResp(errs ...APIError) Response {
resp := Response{Errors: errs}
resp.Code = resp.StatusCode()
return resp
}