/
error.go
58 lines (54 loc) · 1.55 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
51
52
53
54
55
56
57
58
package error
import (
"fmt"
"net/http"
"github.com/evt/rest-api-example/lib/types"
"github.com/labstack/echo/v4"
)
// HTTPError is our HTTP error definition.
type HTTPError struct {
Code int `json:"code"`
Name string `json:"name"`
Message string `json:"message"`
Cause string `json:"cause,omitempty"`
}
// Error implements a custom echo error handler that will encode errors as JSON objects rather than
// just return a text body. It will also make sure to not have the redundant information given in
// the echo string encoding of HTTP errors.
func Error(err error, ctx echo.Context) {
errObj := HTTPError{
Code: http.StatusInternalServerError,
Message: err.Error(),
}
switch err {
case types.ErrBadRequest:
errObj.Code = http.StatusBadRequest
case types.ErrNotFound:
errObj.Code = http.StatusNotFound
case types.ErrDuplicateEntry, types.ErrConflict:
errObj.Code = http.StatusConflict
case types.ErrForbidden:
errObj.Code = http.StatusForbidden
case types.ErrUnprocessableEntity:
errObj.Code = http.StatusUnprocessableEntity
case types.ErrPartialOk:
errObj.Code = http.StatusPartialContent
case types.ErrGone:
errObj.Code = http.StatusGone
case types.ErrUnauthorized:
errObj.Code = http.StatusUnauthorized
}
he, ok := err.(*echo.HTTPError)
if ok {
errObj.Code = he.Code
errObj.Message = fmt.Sprintf("%v", he.Message)
}
errObj.Name = http.StatusText(errObj.Code)
if !ctx.Response().Committed {
if ctx.Request().Method == echo.HEAD {
ctx.NoContent(errObj.Code)
} else {
ctx.JSON(errObj.Code, errObj)
}
}
}