-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
151 lines (120 loc) · 3.53 KB
/
error.go
File metadata and controls
151 lines (120 loc) · 3.53 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
package webapp
import (
"bytes"
"context"
"encoding/xml"
"github.com/mbict/go-webapp/encoding/json"
"log"
"net/http"
"strconv"
)
func E(err error) http.HandlerFunc {
h := H(func(context.Context, Empty) (*Empty, error) {
return nil, err
})
return func(rw http.ResponseWriter, req *http.Request) {
h(rw, req)
}
}
func ErrorHandler() func(error) http.HandlerFunc {
encoderNegotiator := NewNegotiatorBuilder[Encoder]()
jsonEncoder := json.NewJsonEncoding()
encoderNegotiator.Register("application/json", jsonEncoder, "*/*")
return func(e error) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
enc, err := encoderNegotiator.Get(req.Header.Get("Accept"))
//default to json encoder if no negotiation cna be done
if err != nil {
enc = jsonEncoder
}
rw.Header().Add("Content-Type", enc.Mimetype()+"; charset=utf-8")
if h, ok := e.(Headerer); ok {
for k, v := range h.Header() {
rw.Header().Add(k, v[0])
}
}
if sc, ok := e.(StatusCoder); ok {
rw.WriteHeader(sc.StatusCode())
}
if err = enc.Encode(rw, e); err != nil {
log.Default().Printf("%v", err)
http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}
}
}
type HTTPError struct {
err error
code int
}
func Error(err error, code int) error {
if _, ok := err.(StatusCoder); ok {
return err
}
return &HTTPError{err, code}
}
func (e *HTTPError) Error() string {
return e.err.Error()
}
func (e *HTTPError) StatusCode() int {
if sc, ok := e.err.(StatusCoder); ok {
return sc.StatusCode()
}
if e.code == 0 {
return http.StatusInternalServerError
}
return e.code
}
func (e *HTTPError) MarshalText() ([]byte, error) {
return []byte(e.Error()), nil
}
func (e *HTTPError) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
buf.WriteString(`{"message":`)
buf.WriteString(strconv.Quote(e.Error()))
buf.WriteRune('}')
return buf.Bytes(), nil
}
func (e *HTTPError) MarshalXML(enc *xml.Encoder, _ xml.StartElement) error {
start := xml.StartElement{Name: xml.Name{Local: "message"}}
return enc.EncodeElement(e.Error(), start)
}
func (e *HTTPError) MarshalYAML() (interface{}, error) {
return map[string]string{"message": e.Error()}, nil
}
//----
// StatusError creates an error from an HTTP status code.
type StatusError int
const (
ErrBadRequest = StatusError(http.StatusBadRequest)
ErrUnauthorized = StatusError(http.StatusUnauthorized)
ErrForbidden = StatusError(http.StatusForbidden)
ErrNotFound = StatusError(http.StatusNotFound)
ErrMethodNotAllowed = StatusError(http.StatusMethodNotAllowed)
ErrNotAcceptable = StatusError(http.StatusNotAcceptable)
ErrUnsupportedMediaType = StatusError(http.StatusUnsupportedMediaType)
ErrInternalServerError = StatusError(http.StatusInternalServerError)
)
func (e StatusError) Error() string {
return http.StatusText(int(e))
}
func (e StatusError) StatusCode() int {
return int(e)
}
func (e StatusError) MarshalText() ([]byte, error) {
return []byte(e.Error()), nil
}
func (e StatusError) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
buf.WriteString(`{"message":`)
buf.WriteString(strconv.Quote(e.Error()))
buf.WriteRune('}')
return buf.Bytes(), nil
}
func (e StatusError) MarshalXML(enc *xml.Encoder, _ xml.StartElement) error {
start := xml.StartElement{Name: xml.Name{Local: "message"}}
return enc.EncodeElement(e.Error(), start)
}
func (e StatusError) MarshalYAML() (interface{}, error) {
return map[string]string{"message": e.Error()}, nil
}