Add error package
This commit is contained in:
parent
6d66c5e1f4
commit
5d1b234b86
1 changed files with 47 additions and 0 deletions
47
err/err.go
Normal file
47
err/err.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue