-
Notifications
You must be signed in to change notification settings - Fork 8
/
http_error.go
57 lines (46 loc) · 1.67 KB
/
http_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
51
52
53
54
55
56
57
package httperr
import (
"net/http"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/errors"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/logs"
"github.com/go-chi/render"
)
func InternalError(slug string, err error, w http.ResponseWriter, r *http.Request) {
httpRespondWithError(err, slug, w, r, "Internal server error", http.StatusInternalServerError)
}
func Unauthorised(slug string, err error, w http.ResponseWriter, r *http.Request) {
httpRespondWithError(err, slug, w, r, "Unauthorised", http.StatusUnauthorized)
}
func BadRequest(slug string, err error, w http.ResponseWriter, r *http.Request) {
httpRespondWithError(err, slug, w, r, "Bad request", http.StatusBadRequest)
}
func RespondWithSlugError(err error, w http.ResponseWriter, r *http.Request) {
slugError, ok := err.(errors.SlugError)
if !ok {
InternalError("internal-server-error", err, w, r)
return
}
switch slugError.ErrorType() {
case errors.ErrorTypeAuthorization:
Unauthorised(slugError.Slug(), slugError, w, r)
case errors.ErrorTypeIncorrectInput:
BadRequest(slugError.Slug(), slugError, w, r)
default:
InternalError(slugError.Slug(), slugError, w, r)
}
}
func httpRespondWithError(err error, slug string, w http.ResponseWriter, r *http.Request, logMSg string, status int) {
logs.GetLogEntry(r).WithError(err).WithField("error-slug", slug).Warn(logMSg)
resp := ErrorResponse{slug, status}
if err := render.Render(w, r, resp); err != nil {
panic(err)
}
}
type ErrorResponse struct {
Slug string `json:"slug"`
httpStatus int
}
func (e ErrorResponse) Render(w http.ResponseWriter, r *http.Request) error {
w.WriteHeader(e.httpStatus)
return nil
}