Handle handles errors. nolint:goconst // "message" is a stable JSON response field name; extracting a constant adds noise without value
()
| 27 | // |
| 28 | //nolint:goconst // "message" is a stable JSON response field name; extracting a constant adds noise without value |
| 29 | func (h *JsonErrorHandler) Handle() echo.HTTPErrorHandler { |
| 30 | return func(err error, c echo.Context) { |
| 31 | logger := log.CtxLogger(c.Request().Context()) |
| 32 | |
| 33 | if c.Response().Committed { |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | var httpError *echo.HTTPError |
| 38 | if errors.As(err, &httpError) { |
| 39 | if httpError.Internal != nil { |
| 40 | var internalHttpError *echo.HTTPError |
| 41 | if errors.As(httpError.Internal, &internalHttpError) { |
| 42 | httpError = internalHttpError |
| 43 | } |
| 44 | } |
| 45 | } else { |
| 46 | httpError = &echo.HTTPError{ |
| 47 | Code: http.StatusInternalServerError, |
| 48 | Message: err.Error(), |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | var logRespFields map[string]interface{} |
| 53 | |
| 54 | if h.stack { |
| 55 | errStack := "n/a" |
| 56 | if err != nil { |
| 57 | errStack = errors.New(err).ErrorStack() |
| 58 | } |
| 59 | |
| 60 | switch m := httpError.Message.(type) { |
| 61 | case error: |
| 62 | logRespFields = map[string]interface{}{ |
| 63 | "message": m.Error(), |
| 64 | "stack": errStack, |
| 65 | } |
| 66 | default: |
| 67 | logRespFields = map[string]interface{}{ |
| 68 | "message": m, |
| 69 | "stack": errStack, |
| 70 | } |
| 71 | } |
| 72 | } else { |
| 73 | switch m := httpError.Message.(type) { |
| 74 | case error: |
| 75 | logRespFields = map[string]interface{}{ |
| 76 | "message": m.Error(), |
| 77 | } |
| 78 | default: |
| 79 | logRespFields = map[string]interface{}{ |
| 80 | "message": m, |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | logger.Error().Err(err).Fields(logRespFields).Msg("error handler") |
| 86 |