customer/internal/utils/echo_response.go

34 lines
1001 B
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
)
2023-05-17 20:27:09 +00:00
var httpStatuses = map[errors.ErrorType]int{
errors.ErrInternalError: http.StatusInternalServerError,
errors.ErrInvalidArgs: http.StatusBadRequest,
errors.ErrNoAccess: http.StatusForbidden,
errors.ErrNotFound: http.StatusNotFound,
errors.ErrMethodNotImplemented: http.StatusNotImplemented,
errors.ErrConflict: http.StatusConflict,
2023-05-16 01:12:07 +00:00
}
2023-05-17 20:27:09 +00:00
func DetermineEchoErrorResponse(ctx echo.Context, err errors.Error) error {
status, ok := httpStatuses[err.Type()]
2023-05-16 01:12:07 +00:00
if !ok {
return ctx.JSON(http.StatusInternalServerError, models.ResponseErrorHTTP{
StatusCode: http.StatusInternalServerError,
2023-05-17 20:27:09 +00:00
Message: err.Error(),
2023-05-16 01:12:07 +00:00
})
}
return ctx.JSON(status, models.ResponseErrorHTTP{
StatusCode: status,
2023-05-17 20:27:09 +00:00
Message: err.Error(),
2023-05-16 01:12:07 +00:00
})
}