From 547698872d621994d1e7264afff315e664b4a8e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Murta?= Date: Sun, 24 Aug 2025 23:25:24 +0100 Subject: [PATCH] refactor: return err from API handlers Instead of logging + NoContent(http.StatusIntervalServerError). Gin already logs the error when returned and returns a 500 to the client. --- internal/api/impl.go | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/internal/api/impl.go b/internal/api/impl.go index a8cc21c..c379160 100644 --- a/internal/api/impl.go +++ b/internal/api/impl.go @@ -21,7 +21,7 @@ func (server *ServerImpl) GetBanks(ctx echo.Context) error { banks, err := server.Dal.Banks() if err != nil { - return ctx.NoContent(http.StatusInternalServerError) + return err } if len(banks) == 0 { @@ -36,8 +36,7 @@ func (server *ServerImpl) GetBankById(ctx echo.Context, bankId string) error { bank, err := server.Dal.Bank(bankId) if err != nil { - log.Printf("%v", err) - return ctx.NoContent(http.StatusInternalServerError) + return err } if bank == nil { @@ -78,8 +77,7 @@ func (server *ServerImpl) GetTransactionById(ctx echo.Context, transactionId int transaction, err := server.Dal.Transaction(transactionId) if err != nil { - log.Printf("%v", err) - return ctx.NoContent(http.StatusInternalServerError) + return err } if transaction == nil { @@ -98,8 +96,7 @@ func (server *ServerImpl) CreateTransaction(ctx echo.Context) error { transaction, err := server.Dal.InsertTransaction(transaction2entity(*t)) if err != nil { - log.Printf("%v", err) - return ctx.NoContent(http.StatusInternalServerError) + return err } return ctx.JSON(http.StatusCreated, entity2transaction(transaction)) @@ -107,8 +104,7 @@ func (server *ServerImpl) CreateTransaction(ctx echo.Context) error { func (server *ServerImpl) UpdateTransaction(ctx echo.Context, transactionId int64) error { if exists, err := server.Dal.TransactionExists(entity.TransactionId(transactionId)); err != nil { - log.Printf("%v", err) - return ctx.NoContent(http.StatusInternalServerError) + return err } else if !exists { return ctx.NoContent(http.StatusNotFound) } @@ -121,8 +117,7 @@ func (server *ServerImpl) UpdateTransaction(ctx echo.Context, transactionId int6 success, err := server.Dal.UpdateTransaction(entity.TransactionId(transactionId), update.Category) if err != nil { - log.Printf("%v", err) - return ctx.NoContent(http.StatusInternalServerError) + return err } if !success { @@ -134,7 +129,7 @@ func (server *ServerImpl) UpdateTransaction(ctx echo.Context, transactionId int6 func (server *ServerImpl) GetCategories(ctx echo.Context) error { categories, err := server.Dal.Categories() if err != nil { - return ctx.NoContent(http.StatusInternalServerError) + return err } if len(categories) == 0 {