-
Notifications
You must be signed in to change notification settings - Fork 1
/
responder.go
103 lines (88 loc) · 2.26 KB
/
responder.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
package pocket
import (
"bytes"
"io"
"io/ioutil"
"net/http"
)
var _ = (Responder)(&BasicResponder{})
// Responder describes an error type that can resolve to a HTTP response. This
// means providing a response code and a body.
type Responder interface {
error
Headers() http.Header
Body() io.ReadCloser
Status() int
}
// BasicResponder provides a basic implementation for the Responder interface
// for writing simple responses.
type BasicResponder struct {
H http.Header
R io.ReadCloser
S int
}
// Error implements the error interface
func (r BasicResponder) Error() string {
if r.R != nil {
b, err := ioutil.ReadAll(r.R)
if err != nil {
panic(err)
}
return string(b)
}
return "(none)"
}
// Headers implements Responder
func (r BasicResponder) Headers() http.Header {
return r.H
}
// Body implements Responder
func (r BasicResponder) Body() io.ReadCloser {
return r.R
}
// Status implements Responder
func (r BasicResponder) Status() int {
return r.S
}
// -
// Basic responder helpers
// -
// OK returns a responder with HTTP 200 OK default
func OK() BasicResponder {
return BasicResponder{S: http.StatusOK}
}
// ErrInternalServerError returns a responder for generic internal server errors
func ErrInternalServerError(e error) BasicResponder {
return BasicResponder{
S: http.StatusInternalServerError,
R: ioutil.NopCloser(bytes.NewBufferString(e.Error())),
}
}
// ErrUnauthorized returns a responder for generic authorization errors
func ErrUnauthorized(e error) BasicResponder {
return BasicResponder{
S: http.StatusUnauthorized,
R: ioutil.NopCloser(bytes.NewBufferString(e.Error())),
}
}
// ErrForbidden returns a responder for generic forbidden errors
func ErrForbidden(e error) BasicResponder {
return BasicResponder{
S: http.StatusForbidden,
R: ioutil.NopCloser(bytes.NewBufferString(e.Error())),
}
}
// ErrNotFound returns a responder for generic not found errors
func ErrNotFound(e error) BasicResponder {
return BasicResponder{
S: http.StatusNotFound,
R: ioutil.NopCloser(bytes.NewBufferString(e.Error())),
}
}
// ErrBadRequest returns a responder for generic request errors
func ErrBadRequest(e error) BasicResponder {
return BasicResponder{
S: http.StatusBadRequest,
R: ioutil.NopCloser(bytes.NewBufferString(e.Error())),
}
}