delphi.market/server/error.go

21 lines
465 B
Go
Raw Normal View History

package server
import (
"net/http"
2023-11-26 17:21:26 +00:00
"strings"
"github.com/labstack/echo/v4"
)
func httpErrorHandler(err error, c echo.Context) {
c.Logger().Error(err)
code := http.StatusInternalServerError
if httpError, ok := err.(*echo.HTTPError); ok {
code = httpError.Code
}
2023-12-03 05:21:57 +00:00
if strings.Contains(err.Error(), "violates check constraint") || strings.Contains(err.Error(), "violates unique constraint") {
2023-11-26 17:21:26 +00:00
code = 400
}
2023-11-26 17:21:26 +00:00
c.JSON(code, map[string]any{"status": code})
}