generated from PenaSide/GolangTemplate
39 lines
1.3 KiB
Go
39 lines
1.3 KiB
Go
package utils
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"penahub.gitlab.yandexcloud.net/pena-services/pena-social-auth/internal/errors"
|
|
"penahub.gitlab.yandexcloud.net/pena-services/pena-social-auth/internal/models"
|
|
)
|
|
|
|
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,
|
|
})
|
|
}
|