This commit is contained in:
43
internal/utils/error.go
Normal file
43
internal/utils/error.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/swaggest/rest"
|
||||
)
|
||||
|
||||
// CustomApplicationError is a marker interface that tells error middleware to serialize this error as-is, using Json schema.
|
||||
type CustomApplicationError interface {
|
||||
rest.ErrWithHTTPStatus
|
||||
CustomApplicationError()
|
||||
}
|
||||
|
||||
type ApplicationError struct {
|
||||
Message string `json:"message"`
|
||||
code int
|
||||
}
|
||||
|
||||
var (
|
||||
_ CustomApplicationError = ApplicationError{}
|
||||
)
|
||||
|
||||
func NewApplicationError(message string, code int) ApplicationError {
|
||||
return ApplicationError{
|
||||
Message: message,
|
||||
code: code,
|
||||
}
|
||||
}
|
||||
|
||||
func (e ApplicationError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
func (e ApplicationError) HTTPStatus() int {
|
||||
return e.code
|
||||
}
|
||||
|
||||
func (e ApplicationError) CustomApplicationError() {}
|
||||
|
||||
func ValidationError(message string) ApplicationError {
|
||||
return NewApplicationError(message, http.StatusUnprocessableEntity)
|
||||
}
|
||||
Reference in New Issue
Block a user