-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
64 lines (51 loc) · 1.18 KB
/
errors.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
package jsonapi
import (
"errors"
"fmt"
"net/http"
)
type ErrorHandlerFunc = func(w http.ResponseWriter, req *http.Request, err error)
var ErrEmptyBody = errors.New("request body is empty")
var ErrArgumentResolution = errors.New("argument resolution error")
var ErrArgumentUnsupported = errors.New("argument resolution unsupported")
var HandleError ErrorHandlerFunc = handleError
func handleError(w http.ResponseWriter, req *http.Request, err error) {
SendResponse(w, req, err)
}
// Wrapper mimics the unwrap functionality in go errors package
type Wrapper interface {
Unwrap() error
}
// Coder yields the status code for an error
type Coder interface {
Code() int
}
type apiError struct {
code int
msg string
prev error
}
func (e *apiError) Unwrap() error {
return e.prev
}
func (e *apiError) Error() string {
return e.msg
}
func (e *apiError) Code() int {
return e.code
}
func panicToError(v interface{}) (err error) {
switch t := v.(type) {
case string:
err = errors.New(t)
case error:
err = t
default:
err = errors.New(fmt.Sprintf("%v", v))
}
return &apiError{
code: http.StatusInternalServerError,
msg: "An unexpected error has occurred",
prev: err,
}
}