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:
1 parent
bc7d406e41
commit
0ec2425f78
9 files changed
+67
-21
No files matched your search
@@ -75,14 +75,14 @@ func (h *TokenHandler) passwordValid(password string) bool {
|
||||
func (h *TokenHandler) CreateTokenHandler(c *echo.Context) error {
|
||||
var req TokenRequest
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request body"})
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
if !h.passwordValid(req.Password) {
|
||||
logging.GetLogger().Warn("Invalid password attempt for token creation",
|
||||
zap.String("ip", c.RealIP()),
|
||||
zap.String("user_agent", c.Request().UserAgent()))
|
||||
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "Invalid password"})
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid password")
|
||||
}
|
||||
|
||||
if req.ClientType == "" {
|
||||
@@ -92,7 +92,7 @@ func (h *TokenHandler) CreateTokenHandler(c *echo.Context) error {
|
||||
token, err := generateToken()
|
||||
if err != nil {
|
||||
logging.GetLogger().Error("Failed to generate token", zap.Error(err))
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to generate token"})
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate token")
|
||||
}
|
||||
|
||||
session, err := h.q.CreateSession(c.Request().Context(), repository.CreateSessionParams{
|
||||
@@ -104,7 +104,7 @@ func (h *TokenHandler) CreateTokenHandler(c *echo.Context) error {
|
||||
})
|
||||
if err != nil {
|
||||
logging.GetLogger().Error("Failed to create session", zap.Error(err))
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to create session"})
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create session")
|
||||
}
|
||||
|
||||
clientType := req.ClientType
|
||||
@@ -131,16 +131,16 @@ func (h *TokenHandler) CreateTokenHandler(c *echo.Context) error {
|
||||
// @Router /api/v1/token [delete]
|
||||
func (h *TokenHandler) DeleteTokenHandler(c *echo.Context) error {
|
||||
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 := middleware.BearerToken(c)
|
||||
if !ok {
|
||||
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "Invalid authorization format"})
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid authorization format")
|
||||
}
|
||||
|
||||
if err := h.q.DeleteSession(c.Request().Context(), token); err != nil {
|
||||
logging.GetLogger().Error("Failed to delete session", zap.Error(err))
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to invalidate token"})
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to invalidate token")
|
||||
}
|
||||
return c.JSON(http.StatusOK, map[string]string{"status": "token invalidated"})
|
||||
}
|
||||
@@ -159,7 +159,7 @@ func (h *TokenHandler) DeleteTokenHandler(c *echo.Context) error {
|
||||
func (h *TokenHandler) CleanupExpiredSessionsHandler(c *echo.Context) error {
|
||||
if err := h.q.DeleteExpiredSessions(c.Request().Context()); err != nil {
|
||||
logging.GetLogger().Error("Failed to cleanup sessions", zap.Error(err))
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to cleanup sessions"})
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to cleanup sessions")
|
||||
}
|
||||
return c.JSON(http.StatusOK, map[string]string{"status": "cleanup complete"})
|
||||
}
|
||||
Reference in new issue
Block a user