1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-11-24 23:24:00 +02:00

[#2957] added support for wrapped api errors

This commit is contained in:
Gani Georgiev
2023-07-20 22:01:58 +03:00
parent ac52befb5b
commit 1e4c665b53
3 changed files with 100 additions and 10 deletions

View File

@@ -52,6 +52,10 @@ func InitApi(app core.App) (*echo.Echo, error) {
// custom error handler
e.HTTPErrorHandler = func(c echo.Context, err error) {
if err == nil {
return // no error
}
if c.Response().Committed {
if app.IsDebug() {
log.Println("HTTPErrorHandler response was already committed:", err)
@@ -61,24 +65,22 @@ func InitApi(app core.App) (*echo.Echo, error) {
var apiErr *ApiError
switch v := err.(type) {
case *echo.HTTPError:
if errors.As(err, &apiErr) {
if app.IsDebug() && apiErr.RawData() != nil {
log.Println(apiErr.RawData())
}
} else if v := new(echo.HTTPError); errors.As(err, &v) {
if v.Internal != nil && app.IsDebug() {
log.Println(v.Internal)
}
msg := fmt.Sprintf("%v", v.Message)
apiErr = NewApiError(v.Code, msg, v)
case *ApiError:
if app.IsDebug() && v.RawData() != nil {
log.Println(v.RawData())
}
apiErr = v
default:
if err != nil && app.IsDebug() {
} else {
if app.IsDebug() {
log.Println(err)
}
if err != nil && errors.Is(err, sql.ErrNoRows) {
if errors.Is(err, sql.ErrNoRows) {
apiErr = NewNotFoundError("", err)
} else {
apiErr = NewBadRequestError("", err)