-
Notifications
You must be signed in to change notification settings - Fork 5
/
error.go
50 lines (41 loc) · 1.17 KB
/
error.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
package http
import (
"fmt"
"net/http"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type Error struct {
Message string `json:"message"`
}
type VerboseError struct {
Class string
Message string
Method string
Endpoint string
OriginalError error
}
func NewVerboseError(class string, r *http.Request, err error) *VerboseError {
return &VerboseError{
Class: class,
Method: r.Method,
Endpoint: r.URL.Path,
Message: err.Error(),
OriginalError: err,
}
}
func (v *VerboseError) Error() string {
return fmt.Sprintf("%s %s: %s", v.Method, v.Endpoint, v.Message)
}
func NewNotFoundError(r *http.Request) *VerboseError {
return NewVerboseError("NotFoundError", r, errors.New("endpoint not implemented"))
}
func WriteNotFoundError(err error, w http.ResponseWriter, log *logrus.Entry) {
log.Error(err)
ErrorResponse(w, err.Error(), http.StatusNotFound)
}
func WriteInternalError(w http.ResponseWriter, verr *VerboseError, r *http.Request, log *logrus.Entry) {
log.WithError(verr).Errorf("%s handler error", r.URL.Path)
log.Errorf("%+v", verr.OriginalError)
ErrorResponse(w, verr.Error(), http.StatusBadRequest)
}