Return every error as {"error": "..."}

Echo errors, middleware and handlers used three different formats (message object, error object, bare string). One error handler now writes all of them the same way; status codes are unchanged.
This commit is contained in:
Sansan committed 2026-10-06 16:21:14 +02:00
1 parent bc7d406e41
commit 0ec2425f78
9 files changed
+67 -21

No files matched your search

+1 -3
View File
@@ -23,9 +23,7 @@ func RateLimit(limit int, window time.Duration) echo.MiddlewareFunc {
logging.GetLogger().Warn("Rate limit exceeded",
zap.String("ip", identifier),
zap.String("path", c.Request().URL.Path))
return c.JSON(http.StatusTooManyRequests, map[string]string{
"error": "Rate limit exceeded. Please try again later.",
})
return echo.NewHTTPError(http.StatusTooManyRequests, "Rate limit exceeded. Please try again later.")
}
return echoMiddleware.RateLimiterWithConfig(config)
}
+1 -1
View File
@@ -14,7 +14,7 @@ func SyncCheck(isSyncing func() bool) echo.MiddlewareFunc {
return func(c *echo.Context) error {
if isSyncing() {
logging.GetLogger().Info("Syncing is in progress - request blocked")
return c.JSON(http.StatusLocked, "Syncing is in progress")
return echo.NewHTTPError(http.StatusLocked, "Syncing is in progress")
}
return next(c)
}
+4 -4
View File
@@ -28,11 +28,11 @@ func TokenAuth(q repository.Querier, disabled bool) echo.MiddlewareFunc {
}
if c.Request().Header.Get("Authorization") == "" {
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "Authorization header required"})
return echo.NewHTTPError(http.StatusUnauthorized, "Authorization header required")
}
token, ok := BearerToken(c)
if !ok {
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "Invalid authorization format. Use: Bearer <token>"})
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid authorization format. Use: Bearer <token>")
}
ctx := c.Request().Context()
@@ -42,7 +42,7 @@ func TokenAuth(q repository.Querier, disabled bool) echo.MiddlewareFunc {
zap.String("ip", c.RealIP()),
zap.String("error", err.Error()),
)
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "Invalid or expired token"})
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid or expired token")
}
if time.Now().After(session.ExpiresAt.Time) {
@@ -52,7 +52,7 @@ func TokenAuth(q repository.Querier, disabled bool) echo.MiddlewareFunc {
logging.GetLogger().Error("Failed to delete expired session", zap.Error(err))
}
}(context.WithoutCancel(ctx))
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "Token expired"})
return echo.NewHTTPError(http.StatusUnauthorized, "Token expired")
}
c.Set("session", session)