first commit
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package slerrors
|
||||
|
||||
type badRequestError struct {
|
||||
Text string
|
||||
}
|
||||
|
||||
func (err *badRequestError) Error() string {
|
||||
return "validation failed: " + err.Text
|
||||
}
|
||||
|
||||
func BadRequest(text string) error {
|
||||
return &badRequestError{Text: text}
|
||||
}
|
||||
|
||||
func IsBadRequest(err error) bool {
|
||||
_, ok := err.(*badRequestError)
|
||||
return ok
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package slerrors
|
||||
|
||||
type forbiddenError struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e *forbiddenError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
func Forbidden(message string) error {
|
||||
return &forbiddenError{Message: message}
|
||||
}
|
||||
|
||||
func IsForbidden(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
_, ok := err.(*forbiddenError)
|
||||
return ok
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package slerrors
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ErrorResponse struct {
|
||||
Code int `json:"errorCode"`
|
||||
Message string `json:"errorMessage"`
|
||||
}
|
||||
|
||||
func Respond(c *gin.Context, err error) {
|
||||
if IsNotFound(err) {
|
||||
c.JSON(http.StatusNotFound, ErrorResponse{
|
||||
Code: http.StatusNotFound,
|
||||
Message: err.Error(),
|
||||
})
|
||||
} else if IsForbidden(err) {
|
||||
c.JSON(http.StatusForbidden, ErrorResponse{
|
||||
Code: http.StatusForbidden,
|
||||
Message: err.Error(),
|
||||
})
|
||||
} else if IsBadRequest(err) {
|
||||
c.JSON(http.StatusBadRequest, ErrorResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Message: err.Error(),
|
||||
})
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, ErrorResponse{
|
||||
Code: http.StatusInternalServerError,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package slerrors
|
||||
|
||||
type notFoundError struct {
|
||||
Subject string
|
||||
}
|
||||
|
||||
func (err *notFoundError) Error() string {
|
||||
return err.Subject + " not found"
|
||||
}
|
||||
|
||||
func NotFound(subject string) error {
|
||||
return ¬FoundError{Subject: subject}
|
||||
}
|
||||
|
||||
func IsNotFound(err error) bool {
|
||||
_, ok := err.(*notFoundError)
|
||||
return ok
|
||||
}
|
||||
Reference in New Issue
Block a user