-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrhandler.go
More file actions
52 lines (49 loc) · 1.56 KB
/
errhandler.go
File metadata and controls
52 lines (49 loc) · 1.56 KB
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
package ctx
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/labstack/echo"
"github.com/pkg/errors"
"gopkg.in/go-playground/validator.v9"
)
func HTTPErrorHandler(err error, c echo.Context) {
if he, ok := err.(*GErrCall); ok {
err = errors.WithStack(he)
b, _ := json.Marshal(he.ResponseParams)
c.JSONBlob(he.HttpStatus, b)
} else if he, ok := err.(*GError); ok {
err = errors.WithStack(he)
gErrs := CustomCtx{}.GResp().Errors(he)
b, _ := json.Marshal(gErrs.ResponseParams)
c.JSONBlob(gErrs.HttpStatus, b)
} else if he, ok := err.(*echo.HTTPError); ok {
// warp echo error struct
err = errors.WithStack(he)
gErrs := CustomCtx{}.GResp().Errors(&GError{
Code: uint(he.Code),
Message: fmt.Sprintf("%+v", he.Message),
})
b, _ := json.Marshal(gErrs.ResponseParams)
c.JSONBlob(gErrs.HttpStatus, b)
} else if _, ok := err.(*validator.InvalidValidationError); !ok {
var errMsg []string
for _, err := range err.(validator.ValidationErrors) {
errMsg = append(errMsg, fmt.Sprintf("%s:%s", err.Field(), err.ActualTag()))
}
gErrs := CustomCtx{}.GResp(http.StatusBadRequest).Errors(&GError{Code: http.StatusBadRequest, Message: strings.Join(errMsg, ",")})
b, _ := json.Marshal(gErrs.ResponseParams)
c.JSONBlob(gErrs.HttpStatus, b)
} else {
// define unknown error message
err = errors.New("unknown error")
gErrs := CustomCtx{}.GResp().Errors(&GError{
Code: http.StatusInternalServerError,
Message: err.Error(),
})
b, _ := json.Marshal(gErrs.ResponseParams)
c.JSONBlob(gErrs.HttpStatus, b)
}
c.Logger().Error(err)
}