From c7f6b3253c1ddeb1f34a6dbf6e2e5d9b71a6da36 Mon Sep 17 00:00:00 2001 From: Jules Michael Date: Thu, 31 Mar 2022 11:51:48 +0400 Subject: [PATCH] fix(http): proper error messages in middlewares --- http/middleware.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/http/middleware.go b/http/middleware.go index 5f7f81a..8ed4785 100644 --- a/http/middleware.go +++ b/http/middleware.go @@ -9,7 +9,18 @@ import ( func (s *Server) unauthenticatedOnly() gin.HandlerFunc { return func(c *gin.Context) { if s.spoty.IsAuth() { - c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "you are already authenticated"}) + rErr := NewError( + "already-authenticated", + "You are already authenticated.", + http.StatusForbidden, + "You cannot authenticate again as you are already authenticated.", + c.Request.URL.String(), + nil, + ) + + ctx := c.Request.Context() + s.logger.ErrorwContext(ctx, "failed to authenticate", "error", rErr.Error()) + c.AbortWithStatusJSON(http.StatusForbidden, rErr) return } @@ -21,11 +32,19 @@ func (s *Server) unauthenticatedOnly() gin.HandlerFunc { func (s *Server) authenticatedOnly() gin.HandlerFunc { return func(c *gin.Context) { if !s.spoty.IsAuth() { - c.AbortWithStatusJSON( + rErr := NewError( + "not-authenticated", + "You do not have access.", http.StatusUnauthorized, - gin.H{"error": "you must be authenticated to access this endpoint"}, + "You cannot access this endpoint because you are not authenticated.", + c.Request.URL.String(), + nil, ) + ctx := c.Request.Context() + s.logger.ErrorwContext(ctx, "failed to access endpoint", "error", rErr.Error()) + c.AbortWithStatusJSON(http.StatusUnauthorized, rErr) + return }