-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
40 lines (36 loc) · 977 Bytes
/
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
package handler
import (
"github.com/go-chi/render"
"net/http"
)
type ErrorResponse struct {
Err error `json:"-"`
StatusCode int `json:"-"`
StatusText string `json:"status_text"`
Message string `json:"message"`
}
var (
ErrMethodNotAllowed = &ErrorResponse{StatusCode: 405, Message: "Method not allowed"}
ErrNotFound = &ErrorResponse{StatusCode: 404, Message: "Resource not found"}
ErrBadRequest = &ErrorResponse{StatusCode: 400, Message: "Bad request"}
)
func (e *ErrorResponse) Render(w http.ResponseWriter, r *http.Request) error {
render.Status(r, e.StatusCode)
return nil
}
func ErrorRenderer(err error) *ErrorResponse {
return &ErrorResponse{
Err: err,
StatusCode: 400,
StatusText: "Bad request",
Message: err.Error(),
}
}
func ServerErrorRenderer(err error) *ErrorResponse {
return &ErrorResponse{
Err: err,
StatusCode: 500,
StatusText: "Internal server error",
Message: err.Error(),
}
}