Use AbortWithStatusJSON instead of AbortWithError
All checks were successful
continuous-integration/drone/pr Build is passing
ci/woodpecker/push/woodpecker Pipeline was successful
continuous-integration/drone/push Build is passing
ci/woodpecker/pr/woodpecker Pipeline was successful

This commit is contained in:
Jan Bader 2022-02-20 20:42:57 +00:00
parent 36b2f12183
commit ca51ac5e27
4 changed files with 23 additions and 16 deletions

View File

@ -55,7 +55,7 @@ func (h *Handler) budgetingForMonth(c *gin.Context) {
budgetID := c.Param("budgetid") budgetID := c.Param("budgetid")
budgetUUID, err := uuid.Parse(budgetID) budgetUUID, err := uuid.Parse(budgetID)
if err != nil { if err != nil {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("budgetid missing from URL")) c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"budgetid missing from URL"})
return return
} }
@ -80,7 +80,7 @@ func (h *Handler) budgetingForMonth(c *gin.Context) {
firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0) firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0)
cumultativeBalances, err := h.Service.GetCumultativeBalances(c.Request.Context(), budgetUUID) cumultativeBalances, err := h.Service.GetCumultativeBalances(c.Request.Context(), budgetUUID)
if err != nil { if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("load balances: %w", err)) c.AbortWithStatusJSON(http.StatusInternalServerError, ErrorResponse{fmt.Sprintf("error loading balances: %s", err)})
return return
} }
@ -115,14 +115,13 @@ func (h *Handler) budgetingForMonth(c *gin.Context) {
AvailableBalance postgres.Numeric AvailableBalance postgres.Numeric
}{categoriesWithBalance, availableBalance} }{categoriesWithBalance, availableBalance}
c.JSON(http.StatusOK, data) c.JSON(http.StatusOK, data)
} }
func (h *Handler) budgeting(c *gin.Context) { func (h *Handler) budgeting(c *gin.Context) {
budgetID := c.Param("budgetid") budgetID := c.Param("budgetid")
budgetUUID, err := uuid.Parse(budgetID) budgetUUID, err := uuid.Parse(budgetID)
if err != nil { if err != nil {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("budgetid missing from URL")) c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"budgetid missing from URL"})
return return
} }

View File

@ -33,6 +33,10 @@ func (h *Handler) Serve() {
} }
} }
type ErrorResponse struct {
Message string
}
// LoadRoutes initializes all the routes. // LoadRoutes initializes all the routes.
func (h *Handler) LoadRoutes(router *gin.Engine) { func (h *Handler) LoadRoutes(router *gin.Engine) {
router.Use(enableCachingForStaticFiles()) router.Use(enableCachingForStaticFiles())

View File

@ -11,16 +11,21 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
func (h *Handler) verifyLogin(c *gin.Context) (budgeteer.Token, error) { const (
tokenString := c.GetHeader("Authorization") HeaderName = "Authorization"
if len(tokenString) < 8 { Bearer = "Bearer "
return nil, fmt.Errorf("no authorization header supplied") )
func (h *Handler) verifyLogin(c *gin.Context) (budgeteer.Token, *ErrorResponse) { //nolint:ireturn
tokenString := c.GetHeader(HeaderName)
if len(tokenString) <= len(Bearer) {
return nil, &ErrorResponse{"no authorization header supplied"}
} }
tokenString = tokenString[7:] tokenString = tokenString[7:]
token, err := h.TokenVerifier.VerifyToken(tokenString) token, err := h.TokenVerifier.VerifyToken(tokenString)
if err != nil { if err != nil {
return nil, fmt.Errorf("verify token '%s': %w", tokenString, err) return nil, &ErrorResponse{fmt.Sprintf("verify token '%s': %s", tokenString, err)}
} }
return token, nil return token, nil
@ -29,8 +34,8 @@ func (h *Handler) verifyLogin(c *gin.Context) (budgeteer.Token, error) {
func (h *Handler) verifyLoginWithForbidden(c *gin.Context) { func (h *Handler) verifyLoginWithForbidden(c *gin.Context) {
token, err := h.verifyLogin(c) token, err := h.verifyLogin(c)
if err != nil { if err != nil {
//c.Header("WWW-Authenticate", "Bearer") // c.Header("WWW-Authenticate", "Bearer")
c.AbortWithError(http.StatusForbidden, err) c.AbortWithStatusJSON(http.StatusForbidden, err)
return return
} }
@ -104,18 +109,18 @@ func (h *Handler) registerPost(c *gin.Context) {
var register registerInformation var register registerInformation
err := c.BindJSON(&register) err := c.BindJSON(&register)
if err != nil { if err != nil {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("parse body: %w", err)) c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"error parsing body"})
return return
} }
if register.Email == "" || register.Password == "" || register.Name == "" { if register.Email == "" || register.Password == "" || register.Name == "" {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("e-mail, password and name are required")) c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"e-mail, password and name are required"})
return return
} }
_, err = h.Service.GetUserByUsername(c.Request.Context(), register.Email) _, err = h.Service.GetUserByUsername(c.Request.Context(), register.Email)
if err == nil { if err == nil {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("email is already taken")) c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"email is already taken"})
return return
} }

View File

@ -1,7 +1,6 @@
package server package server
import ( import (
"fmt"
"net/http" "net/http"
"git.javil.eu/jacob1123/budgeteer/postgres" "git.javil.eu/jacob1123/budgeteer/postgres"
@ -12,7 +11,7 @@ import (
func (h *Handler) importYNAB(c *gin.Context) { func (h *Handler) importYNAB(c *gin.Context) {
budgetID, succ := c.Params.Get("budgetid") budgetID, succ := c.Params.Get("budgetid")
if !succ { if !succ {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("no budget_id specified")) c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"no budget_id specified"})
return return
} }