-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
172 lines (143 loc) · 3.85 KB
/
http.go
File metadata and controls
172 lines (143 loc) · 3.85 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
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()
// Dependencies:
// - HTTPError -> errHTTPStatus
// - HTTPStatus -> (none)
// - httpError -> httpMessage
// - httpCodeError -> HTTPError, 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
}
// 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,
}
}
// If HTTPStatus is implemented by the output or the error returned
// by a handler used in [Handler], it is user as the HTTP Status code
// to be returned.
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())
}
// httpCodeError 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 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)
}
// Output sends a JSON-encoded output.
// If out is an error, it sends a HTTP BadRequest error and a JSON object with a "error" string value.
// If out is a []byte, it outputs it directly.
func Output(w http.ResponseWriter, out any) {
if err, ok := out.(error); ok {
httpError(w, err)
return
}
// if the returned type is a string, output it as a "info" message:
if s, ok := out.(string); ok {
httpMessage(w, http.StatusOK, "info", s)
return
}
// if the returned type is a []byte, output it directly:
if b, ok := out.([]byte); ok {
w.Write(b)
return
}
w.Header().Set("Content-Type", "application/json")
code := http.StatusOK
if hs, ok := out.(HTTPStatus); ok {
code = hs.HTTPStatus()
}
w.WriteHeader(code)
e := json.NewEncoder(w)
err := e.Encode(out)
if err != nil {
fmt.Fprintf(w, "{\"error\": %q}\n", err.Error())
}
}
type outWithHTTPStatus struct {
status int
output any
}
func (o outWithHTTPStatus) HTTPStatus() int {
return o.status
}
func (o outWithHTTPStatus) MarshalJSON() ([]byte, error) {
return json.Marshal(o.output)
}
// OutputWithStatus returns a value with a MarshalJSON that returns the same
// JSON encoding as the original, and with an embedded HTTP status that is
// returned to the client if this value is returned by a handler.
func OutputWithStatus(status int, out any) any {
return outWithHTTPStatus{
status: status,
output: out,
}
}