link-go/err/err.go
2021-03-15 01:22:41 +06:00

47 lines
878 B
Go

package err
import (
"github.com/labstack/echo/v4"
"net/http"
)
type NotFoundError struct {
Message string
}
func (err NotFoundError) Error() string {
return err.Message
}
type UnknownError struct {
Message string
}
func (err UnknownError) Error() string {
return err.Message
}
type AlreadyExistsError struct {
Message string
}
func (err AlreadyExistsError) Error() string {
return err.Message
}
func MapErrToHTTPErr(err interface{}) *echo.HTTPError {
switch v := err.(type) {
case NotFoundError:
return echo.NewHTTPError(http.StatusNotFound, v.Message)
case UnknownError:
if v.Message != "" {
return echo.NewHTTPError(http.StatusInternalServerError, v.Message)
} else {
return echo.NewHTTPError(http.StatusInternalServerError)
}
case AlreadyExistsError:
return echo.NewHTTPError(http.StatusBadRequest, v.Message)
default:
return nil
}
}