|
@@ -4,8 +4,32 @@ import (
|
|
|
"net/http"
|
|
|
)
|
|
|
|
|
|
+
|
|
|
+var (
|
|
|
+ Err = Error{}
|
|
|
+
|
|
|
+ ErrMovedPermanently = NewError(http.StatusMovedPermanently, "")
|
|
|
+ ErrFound = NewError(http.StatusFound, "")
|
|
|
+ ErrTemporaryRedirect = NewError(http.StatusTemporaryRedirect, "")
|
|
|
+ ErrPermamentRedirect = NewError(http.StatusPermanentRedirect, "")
|
|
|
+
|
|
|
+ ErrBadRequest = NewError(http.StatusBadRequest, "")
|
|
|
+ ErrUnauthorized = NewError(http.StatusUnauthorized, "")
|
|
|
+ ErrPaymentRequired = NewError(http.StatusPaymentRequired, "")
|
|
|
+ ErrForbidden = NewError(http.StatusForbidden, "")
|
|
|
+ ErrNotFound = NewError(http.StatusNotFound, "")
|
|
|
+ ErrMethodNotAllowed = NewError(http.StatusMethodNotAllowed, "")
|
|
|
+ ErrNotAcceptable = NewError(http.StatusNotAcceptable, "")
|
|
|
+
|
|
|
+ ErrInternalServerError = NewError(http.StatusInternalServerError, "")
|
|
|
+ ErrNotImplemented = NewError(http.StatusNotImplemented, "")
|
|
|
+ ErrBadGateway = NewError(http.StatusBadGateway, "")
|
|
|
+ ErrServiceUnavailable = NewError(http.StatusServiceUnavailable, "")
|
|
|
+ ErrGatewayTimeout = NewError(http.StatusGatewayTimeout, "")
|
|
|
+)
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
type Error struct {
|
|
|
StatusCode int `json:"statusCode"`
|
|
|
Message string `json:"message"`
|
|
@@ -27,11 +51,8 @@ func (e Error) Error() string {
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
func (e Error) Is(target error) bool {
|
|
|
t, ok := target.(Error)
|
|
|
if !ok {
|
|
@@ -87,3 +108,15 @@ func (e Error) Write(w http.ResponseWriter) {
|
|
|
func (e Error) WriteJSON(w http.ResponseWriter) error {
|
|
|
return WriteResponseJSON(w, e.StatusCode, e)
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func NewError(statusCode int, message string) Error {
|
|
|
+ if len(message) == 0 {
|
|
|
+ message = http.StatusText(statusCode)
|
|
|
+ }
|
|
|
+ return Error{
|
|
|
+ StatusCode: statusCode,
|
|
|
+ Message: message,
|
|
|
+ }
|
|
|
+}
|