/
httputil.go
121 lines (101 loc) · 2.83 KB
/
httputil.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
// Package httputil provides utilities for implementing web frontends and APIs
// with the Go standard library.
package httputil
import (
"errors"
"fmt"
"net/http"
"github.com/golang/gddo/httputil"
"go.uber.org/zap"
)
// ExpectStatus errors if code is not in the allowed list.
func ExpectStatus(code int, allow ...int) error {
for _, a := range allow {
if code == a {
return nil
}
}
return StatusError(code)
}
// Error is an error with an associated HTTP status code.
type Error struct {
Code int
Err error
}
// StatusError builds an error for the given status code, with the standard status text.
func StatusError(code int) Error {
return Error{
Code: code,
Err: errors.New(http.StatusText(code)),
}
}
func (e Error) Error() string {
var reason string
if e.Err != nil {
reason = e.Err.Error()
} else {
reason = http.StatusText(e.Code)
}
return fmt.Sprintf("http status %d: %s", e.Code, reason)
}
// Status returns the associated HTTP status code.
func (e Error) Status() int { return e.Code }
// BadRequest builds a 400 error with StatusBadRequest.
func BadRequest(err error) Error {
return Error{Code: http.StatusBadRequest, Err: err}
}
// NotFound builds a 404 error.
func NotFound() Error {
return Error{Code: http.StatusNotFound}
}
// MethodNotAllowed builds a 405 error.
func MethodNotAllowed(method string) Error {
return Error{
Code: http.StatusMethodNotAllowed,
Err: fmt.Errorf("method %s not allowed", method),
}
}
// InternalServerError builds a 500 error.
func InternalServerError(err error) Error {
return Error{Code: http.StatusInternalServerError, Err: err}
}
// Handler handles a HTTP request and returns a possible error.
type Handler interface {
HandleRequest(w http.ResponseWriter, r *http.Request) error
}
// HandlerFunc adapts a function to the Handler interface.
type HandlerFunc func(w http.ResponseWriter, r *http.Request) error
// HandleRequest calls h.
func (h HandlerFunc) HandleRequest(w http.ResponseWriter, r *http.Request) error {
return h(w, r)
}
// ErrorHandler wraps a HandlerFunc with an error handling layer.
type ErrorHandler struct {
Handler Handler
Log *zap.Logger
}
func (h ErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Delegate to the handler, writing to a buffer.
buf := new(httputil.ResponseBuffer)
err := h.Handler.HandleRequest(buf, r)
// On success, copy buffer to the original writer w.
if err == nil {
err := buf.WriteTo(w)
if err != nil {
h.Log.Error("write http response", zap.Error(err))
}
return
}
// Handle error.
e, ok := err.(Error)
if !ok {
e = InternalServerError(err)
}
h.Log.Error("handle http request", zap.Error(e))
http.Error(w, e.Error(), e.Status())
}
// OK responds with an ok response. Intended for serverless handlers.
func OK(w http.ResponseWriter) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "ok")
}