Skip to content

Commit

Permalink
SwaggerError everywhere (#57)
Browse files Browse the repository at this point in the history
This PR makes SwaggerError type as the only type of Errors in API (removes our custom Error type)
In additional:

extend SwaggerError by Details field that contains some raw errors (like ent: something not found);
move all error messages into 1 place and use these vars in handlers instead of strings;
move some validations from repo to handlers (to generate errors properly);
some updates in int tests: the most of error messages should be checked;
  • Loading branch information
cyrylkuzmin authored Oct 9, 2023
1 parent c50c01f commit b5b95cb
Show file tree
Hide file tree
Showing 48 changed files with 1,255 additions and 790 deletions.
9 changes: 5 additions & 4 deletions internal/handlers/active_area.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/swagger/models"
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/swagger/restapi/operations"
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/swagger/restapi/operations/active_areas"
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/messages"
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/repositories"
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/utils"
"git.epam.com/epm-lstr/epm-lstr-lc/be/pkg/domain"
Expand Down Expand Up @@ -42,17 +43,17 @@ func (area ActiveArea) GetActiveAreasFunc(repository domain.ActiveAreaRepository
orderColumn := utils.GetValueByPointerOrDefaultValue(a.OrderColumn, order.FieldID)
total, err := repository.TotalActiveAreas(ctx)
if err != nil {
area.logger.Error("failed to query total active areas", zap.Error(err))
area.logger.Error(messages.ErrQueryTotalAreas, zap.Error(err))
return active_areas.NewGetAllActiveAreasDefault(http.StatusInternalServerError).
WithPayload(buildErrorPayload(err))
WithPayload(buildInternalErrorPayload(messages.ErrQueryTotalAreas, err.Error()))
}
var e []*ent.ActiveArea
if total > 0 {
e, err = repository.AllActiveAreas(ctx, int(limit), int(offset), orderBy, orderColumn)
if err != nil {
area.logger.Error("failed to query active areas", zap.Error(err))
area.logger.Error(messages.ErrQueryAreas, zap.Error(err))
return active_areas.NewGetAllActiveAreasDefault(http.StatusInternalServerError).
WithPayload(buildErrorPayload(err))
WithPayload(buildInternalErrorPayload(messages.ErrQueryAreas, err.Error()))
}
}
totalAreas := int64(total)
Expand Down
27 changes: 14 additions & 13 deletions internal/handlers/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/swagger/models"
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/swagger/restapi/operations"
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/swagger/restapi/operations/categories"
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/messages"
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/repositories"
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/utils"
"git.epam.com/epm-lstr/epm-lstr-lc/be/pkg/domain"
Expand Down Expand Up @@ -43,9 +44,9 @@ func (c *Category) CreateNewCategoryFunc(repository domain.CategoryRepository) c
ctx := s.HTTPRequest.Context()
createdCategory, err := repository.CreateCategory(ctx, *s.NewCategory)
if err != nil {
c.logger.Error("cant create new category", zap.Error(err))
c.logger.Error(messages.ErrCreateCategory, zap.Error(err))
return categories.NewCreateNewCategoryDefault(http.StatusInternalServerError).
WithPayload(buildStringPayload("cant create new category"))
WithPayload(buildInternalErrorPayload(messages.ErrCreateCategory, err.Error()))
}
return categories.NewCreateNewCategoryCreated().WithPayload(&models.CreateNewCategoryResponse{
Data: mapCategory(createdCategory),
Expand All @@ -63,9 +64,9 @@ func (c *Category) GetAllCategoriesFunc(repository domain.CategoryRepository) ca

total, err := repository.AllCategoriesTotal(ctx)
if err != nil {
c.logger.Error("query total categories error", zap.Error(err))
c.logger.Error(messages.ErrQueryTotalCategories, zap.Error(err))
return categories.NewGetAllCategoriesDefault(http.StatusInternalServerError).
WithPayload(buildStringPayload("cant get total amount of categories"))
WithPayload(buildInternalErrorPayload(messages.ErrQueryTotalCategories, err.Error()))
}
var allCategories []*ent.Category
if total > 0 {
Expand All @@ -82,9 +83,9 @@ func (c *Category) GetAllCategoriesFunc(repository domain.CategoryRepository) ca
}
allCategories, err = repository.AllCategories(ctx, filter)
if err != nil {
c.logger.Error("query all category error", zap.Error(err))
c.logger.Error(messages.ErrQueryCategories, zap.Error(err))
return categories.NewGetAllCategoriesDefault(http.StatusInternalServerError).
WithPayload(buildStringPayload("cant get all categories"))
WithPayload(buildInternalErrorPayload(messages.ErrQueryCategories, err.Error()))
}
}
mappedCategories := make([]*models.Category, len(allCategories))
Expand All @@ -105,9 +106,9 @@ func (c *Category) GetCategoryByIDFunc(repository domain.CategoryRepository) cat
ctx := s.HTTPRequest.Context()
category, err := repository.CategoryByID(ctx, int(s.CategoryID))
if err != nil {
c.logger.Error("failed to get category", zap.Error(err))
c.logger.Error(messages.ErrGetCategory, zap.Error(err))
return categories.NewGetCategoryByIDDefault(http.StatusInternalServerError).
WithPayload(buildStringPayload("failed to get category"))
WithPayload(buildInternalErrorPayload(messages.ErrGetCategory, err.Error()))
}
return categories.NewGetCategoryByIDOK().WithPayload(&models.GetCategoryByIDResponse{
Data: mapCategory(category),
Expand All @@ -120,11 +121,11 @@ func (c *Category) DeleteCategoryFunc(repository domain.CategoryRepository) cate
ctx := s.HTTPRequest.Context()
err := repository.DeleteCategoryByID(ctx, int(s.CategoryID))
if err != nil {
c.logger.Error("delete category failed", zap.Error(err))
c.logger.Error(messages.ErrDeleteCategory, zap.Error(err))
return categories.NewDeleteCategoryDefault(http.StatusInternalServerError).
WithPayload(buildStringPayload("delete category failed"))
WithPayload(buildInternalErrorPayload(messages.ErrDeleteCategory, err.Error()))
}
return categories.NewDeleteCategoryOK().WithPayload("category deleted")
return categories.NewDeleteCategoryOK().WithPayload(messages.MsgCategoryDeleted)
}
}

Expand All @@ -133,9 +134,9 @@ func (c *Category) UpdateCategoryFunc(repository domain.CategoryRepository) cate
ctx := s.HTTPRequest.Context()
updatedCategory, err := repository.UpdateCategory(ctx, int(s.CategoryID), *s.UpdateCategory)
if err != nil {
c.logger.Error("cant update category", zap.Error(err))
c.logger.Error(messages.ErrUpdateCategory, zap.Error(err))
return categories.NewUpdateCategoryDefault(http.StatusInternalServerError).
WithPayload(buildStringPayload("cant update category"))
WithPayload(buildInternalErrorPayload(messages.ErrUpdateCategory, err.Error()))
}

return categories.NewUpdateCategoryOK().WithPayload(&models.UpdateCategoryResponse{
Expand Down
44 changes: 32 additions & 12 deletions internal/handlers/common.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
package handlers

import "git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/swagger/models"
import (
"net/http"

func buildErrorPayload(err error) *models.Error {
return &models.Error{
Data: &models.ErrorData{
Message: err.Error(),
},
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/swagger/models"
)

func buildErrorPayload(code int32, msg string, details string) *models.SwaggerError {
return &models.SwaggerError{
Code: &code,
Message: &msg,
Details: details, // optional field for raw err messages
}
}

func buildStringPayload(msg string) *models.Error {
return &models.Error{
Data: &models.ErrorData{
Message: msg,
},
}
func buildInternalErrorPayload(msg string, details string) *models.SwaggerError {
return buildErrorPayload(http.StatusInternalServerError, msg, details)
}

func buildExFailedErrorPayload(msg string, details string) *models.SwaggerError {
return buildErrorPayload(http.StatusExpectationFailed, msg, details)
}

func buildConflictErrorPayload(msg string, details string) *models.SwaggerError {
return buildErrorPayload(http.StatusConflict, msg, details)
}

func buildNotFoundErrorPayload(msg string, details string) *models.SwaggerError {
return buildErrorPayload(http.StatusNotFound, msg, details)
}

func buildForbiddenErrorPayload(msg string, details string) *models.SwaggerError {
return buildErrorPayload(http.StatusForbidden, msg, details)
}

func buildBadRequestErrorPayload(msg string, details string) *models.SwaggerError {
return buildErrorPayload(http.StatusBadRequest, msg, details)
}
9 changes: 6 additions & 3 deletions internal/handlers/email_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package handlers
import (
"net/http"

"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/swagger/models"
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/swagger/restapi/operations"
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/generated/swagger/restapi/operations/email_confirm"
"git.epam.com/epm-lstr/epm-lstr-lc/be/internal/messages"
"git.epam.com/epm-lstr/epm-lstr-lc/be/pkg/domain"
"github.com/go-openapi/runtime/middleware"
"go.uber.org/zap"
Expand Down Expand Up @@ -33,11 +35,12 @@ func (e emailConfirmHandler) VerifyEmailConfirmTokenFunc() email_confirm.VerifyE
token := s.Token
err := e.emailConfirm.VerifyTokenAndChangeEmail(ctx, token)
if err != nil {
e.logger.Error("Failed to verify email confirmation token", zap.Error(err))
e.logger.Error(messages.ErrEmailConfirm, zap.Error(err))
return email_confirm.NewVerifyEmailConfirmTokenDefault(http.StatusInternalServerError).
WithPayload(buildStringPayload("Failed to verify email confirmation token. Please try again later"))
WithPayload(buildInternalErrorPayload(messages.ErrEmailConfirm, err.Error()))
}

return email_confirm.NewVerifyEmailConfirmTokenOK().WithPayload("You have successfully confirmed new email")
return email_confirm.NewVerifyEmailConfirmTokenOK().WithPayload(
models.EmailConfirmResponse(messages.MsgEmailConfirmed))
}
}
Loading

0 comments on commit b5b95cb

Please sign in to comment.