first commit

This commit is contained in:
2020-12-31 17:49:54 +01:00
commit 2240985aa1
120 changed files with 11574 additions and 0 deletions
+18
View File
@@ -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
}
+22
View File
@@ -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
}
+35
View File
@@ -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(),
})
}
}
+18
View File
@@ -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 &notFoundError{Subject: subject}
}
func IsNotFound(err error) bool {
_, ok := err.(*notFoundError)
return ok
}