Skip to content

Commit

Permalink
fix(server): change redirection to avoid status override error
Browse files Browse the repository at this point in the history
  • Loading branch information
bastean committed Aug 18, 2024
1 parent 19264a3 commit 3cd8e14
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
12 changes: 8 additions & 4 deletions internal/app/server/middleware/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ func Authentication(c *gin.Context) {
return
}

if value, exists := claims[key.UserId]; exists {
c.Set(key.UserId, value)
c.Next()
} else {
value, exists := claims[key.UserId]

if !exists {
errs.AbortErrWithRedirect(c, errs.MissingKey(key.UserId, "Authentication"), "/")
return
}

c.Set(key.UserId, value)

c.Next()
}
60 changes: 46 additions & 14 deletions internal/app/server/middleware/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ package middleware
import (
"net/http"

"github.com/bastean/codexgo/v4/internal/app/server/util/key"
"github.com/bastean/codexgo/v4/internal/app/server/util/reply"
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
"github.com/bastean/codexgo/v4/internal/pkg/service/logger/log"
"github.com/gin-gonic/gin"
)

type ErrorResponse struct {
Status int
*reply.JSON
}

func ErrorHandler(c *gin.Context) {
c.Next()

Expand All @@ -20,21 +26,47 @@ func ErrorHandler(c *gin.Context) {
errInternal *errors.ErrInternal
)

for _, err := range c.Errors {
switch {
case errors.As(err, &errInvalidValue):
c.JSON(http.StatusUnprocessableEntity, &reply.JSON{Message: errInvalidValue.What, Data: errInvalidValue.Why})
case errors.As(err, &errAlreadyExist):
c.JSON(http.StatusConflict, &reply.JSON{Message: errAlreadyExist.What, Data: errAlreadyExist.Why})
case errors.As(err, &errNotExist):
c.JSON(http.StatusNotFound, &reply.JSON{Message: errNotExist.What, Data: errNotExist.Why})
case errors.As(err, &errFailure):
c.JSON(http.StatusBadRequest, &reply.JSON{Message: errFailure.What, Data: errFailure.Why})
case errors.As(err, &errInternal):
c.JSON(http.StatusInternalServerError, &reply.JSON{Message: "Server error. Try again later."})
fallthrough
case err != nil:
if len(c.Errors) == 0 {
return
}

var response *ErrorResponse

err := c.Errors[len(c.Errors)-1]

switch {
case errors.As(err, &errInvalidValue):
response = &ErrorResponse{http.StatusUnprocessableEntity, &reply.JSON{Message: errInvalidValue.What, Data: errInvalidValue.Why}}
case errors.As(err, &errAlreadyExist):
response = &ErrorResponse{http.StatusConflict, &reply.JSON{Message: errAlreadyExist.What, Data: errAlreadyExist.Why}}
case errors.As(err, &errNotExist):
response = &ErrorResponse{http.StatusNotFound, &reply.JSON{Message: errNotExist.What, Data: errNotExist.Why}}
case errors.As(err, &errFailure):
response = &ErrorResponse{http.StatusBadRequest, &reply.JSON{Message: errFailure.What, Data: errFailure.Why}}
case errors.As(err, &errInternal):
response = &ErrorResponse{http.StatusInternalServerError, &reply.JSON{Message: "Server error. Try again later."}}
fallthrough
case err != nil:
log.Error(err.Error())
}

if route, shouldRedirect := c.Get(key.Redirect); shouldRedirect {
if response != nil {
log.Error(err.Error())
}

route, ok := route.(string)

if !ok {
route = "/"
}

c.Redirect(http.StatusFound, route)

return
}

if response != nil {
c.JSON(response.Status, response.JSON)
}
}
3 changes: 2 additions & 1 deletion internal/app/server/util/errs/abort.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package errs
import (
"net/http"

"github.com/bastean/codexgo/v4/internal/app/server/util/key"
"github.com/gin-gonic/gin"
)

Expand All @@ -13,7 +14,7 @@ func AbortErr(c *gin.Context, err error) {

func AbortErrWithRedirect(c *gin.Context, err error, route string) {
AbortErr(c, err)
c.Redirect(http.StatusFound, route)
c.Set(key.Redirect, route)
}

func AbortWithRedirect(c *gin.Context, route string) {
Expand Down
3 changes: 2 additions & 1 deletion internal/app/server/util/key/key.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package key

var (
const (
Authorization = "Authorization"
Exp = "exp"
Id = "id"
Redirect = "Redirect"
UserId = "userId"
)

0 comments on commit 3cd8e14

Please sign in to comment.