customer/internal/utils/echo_response.go

39 lines
1.3 KiB
Go
Raw Normal View History

2023-05-16 01:12:07 +00:00
package utils
import (
"net/http"
"github.com/labstack/echo/v4"
2023-05-16 04:01:55 +00:00
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
2023-05-16 01:12:07 +00:00
)
var httpStatuses = map[error]int{
errors.ErrEmptyArgs: http.StatusInternalServerError,
errors.ErrInvalidArgs: http.StatusBadRequest,
errors.ErrFindRecord: http.StatusInternalServerError,
errors.ErrInsertRecord: http.StatusInternalServerError,
errors.ErrMethodNotImplemented: http.StatusNotImplemented,
errors.ErrNoRecord: http.StatusNotFound,
errors.ErrNoServerItem: http.StatusNotFound,
errors.ErrTransaction: http.StatusInternalServerError,
errors.ErrTransactionSessionStart: http.StatusInternalServerError,
errors.ErrUpdateRecord: http.StatusInternalServerError,
errors.ErrInvalidReturnValue: http.StatusInternalServerError,
}
func DetermineEchoErrorResponse(ctx echo.Context, err error, message string) error {
status, ok := httpStatuses[err]
if !ok {
return ctx.JSON(http.StatusInternalServerError, models.ResponseErrorHTTP{
StatusCode: http.StatusInternalServerError,
Message: message,
})
}
return ctx.JSON(status, models.ResponseErrorHTTP{
StatusCode: status,
Message: message,
})
}