-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
159 lines (134 loc) · 3.44 KB
/
http.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package api
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
)
// Exported functions:
// - func HTTPError(code int, f any, a ...any) error
// Exported types:
// - type HTTPStatus interface { ... }
// These functions are used by other files in this package:
// - httpError()
// - httpCodeError()
// - httpInfo()
// - httpJSON()
// Dependencies:
// - HTTPError -> errHTTPStatus
// - HTTPStatus -> (none)
// - httpError -> httpMessage
// - httpCodeError -> HTTPError, httpError
// - httpInfo -> httpMessage
// - httpJSON -> (none)
// - apiError -> errHTTPStatus, HTTPError
// - httpMessage -> (none)
// Errors...:
type errHTTPStatus struct {
Status int
Err error
}
func (e errHTTPStatus) Error() string {
return e.Err.Error()
}
func (e errHTTPStatus) Unwrap() error {
return e.Err
}
func (e errHTTPStatus) HTTPStatus() int {
return e.Status
}
// Error returns an errHTTPStatus from another error or a printf-like string.
// The default HTTP status code is BadRequest.
func apiError(f any, a ...any) error {
if err, ok := f.(errHTTPStatus); ok {
return err
}
var err error
if e, ok := f.(error); ok {
err = e
} else if s, ok := f.(string); ok {
err = fmt.Errorf(s, a...)
} else {
err = errors.New(fmt.Sprint(f))
}
code := http.StatusBadRequest
switch {
case errors.Is(err, sql.ErrNoRows):
code = http.StatusNotFound
err = errors.New("not found")
}
return HTTPError(code, err)
}
// HTTPError returns an error with an embedded HTTP status code
func HTTPError(code int, f any, a ...any) error {
var err error
if e, ok := f.(error); ok {
err = e
} else if s, ok := f.(string); ok && len(a) > 0 {
err = fmt.Errorf(s, a...)
} else {
err = errors.New(fmt.Sprint(f))
}
return errHTTPStatus{
Status: code,
Err: err,
}
}
type HTTPStatus interface {
HTTPStatus() int
}
// httpError sends a HTTP error as a response.
//
// If the error returned by the function implements HTTPStatus,
// it is used as the HTTP Status code to be returned.
func httpError(w http.ResponseWriter, f any, a ...any) {
var err error
if e, ok := f.(error); ok {
err = e
} else if s, ok := f.(string); ok {
err = fmt.Errorf(s, a...)
} else {
err = errors.New(fmt.Sprint(f))
}
var es interface{ SQLState() string }
if errors.As(err, &es) {
w.Header().Set("X-SQL-Error", fmt.Sprintf("%s %s", es.SQLState(), err.Error()))
}
var eh HTTPStatus
code := http.StatusBadRequest
switch {
case errors.As(err, &eh):
code = eh.HTTPStatus()
case errors.Is(err, sql.ErrNoRows):
code = http.StatusNotFound
err = errors.New("not found")
}
httpMessage(w, code, "error", err.Error())
}
// httpError sends a HTTP error as a response
func httpCodeError(w http.ResponseWriter, code int, f any, a ...any) {
err := HTTPError(code, f, a...).(errHTTPStatus)
httpError(w, err)
}
func httpInfo(w http.ResponseWriter, msg any) {
httpMessage(w, http.StatusOK, "info", fmt.Sprint(msg))
}
func httpMessage(w http.ResponseWriter, code int, label string, msg string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
fmt.Fprintf(w, "{%q: %q}\n", label, msg)
}
func httpJSON(w http.ResponseWriter, output any, codes ...int) {
code := http.StatusOK
if len(codes) > 0 {
code = codes[0]
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
e := json.NewEncoder(w)
err := e.Encode(output)
if err != nil {
fmt.Fprintf(w, "{\"error\": %q}\n", err.Error())
}
}