Files
epic-ent/internal/exception/handler.go

35 lines
676 B
Go

package exception
import (
"fmt"
"net/http"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
"epic-ent/internal/domain/vo"
)
func RegisterErrorHandler(e *echo.Echo, logger *zap.Logger) {
e.HTTPErrorHandler = func(err error, c echo.Context) {
code := http.StatusInternalServerError
msg := err.Error()
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code
if he.Message != nil {
msg = he.Message.(string)
}
}
verbose := fmt.Sprintf("%+v", err)
logger.With(
zap.Int("status", code),
zap.String("path", c.Path()),
zap.String("method", c.Request().Method),
zap.Error(err),
).Error(verbose)
_ = c.JSON(code, vo.Error(msg))
}
}