2019-09-04 15:22:27 +03:00
|
|
|
package core
|
|
|
|
|
|
|
|
import "net/http"
|
|
|
|
|
|
|
|
// ErrorResponse struct
|
|
|
|
type ErrorResponse struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
2019-09-12 11:49:41 +03:00
|
|
|
// GetErrorResponse returns ErrorResponse with specified status code
|
|
|
|
// Usage (with gin):
|
|
|
|
// context.JSON(GetErrorResponse(http.StatusPaymentRequired, "Not enough money"))
|
|
|
|
func GetErrorResponse(statusCode int, error string) (int, interface{}) {
|
|
|
|
return statusCode, ErrorResponse{
|
|
|
|
Error: error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:22:27 +03:00
|
|
|
// BadRequest returns ErrorResponse with code 400
|
|
|
|
// Usage (with gin):
|
|
|
|
// context.JSON(BadRequest("invalid data"))
|
|
|
|
func BadRequest(error string) (int, interface{}) {
|
2019-09-12 11:49:41 +03:00
|
|
|
return GetErrorResponse(http.StatusBadRequest, error)
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
2019-09-12 11:49:41 +03:00
|
|
|
|
|
|
|
// InternalServerError returns ErrorResponse with code 500
|
|
|
|
// Usage (with gin):
|
|
|
|
// context.JSON(BadRequest("invalid data"))
|
|
|
|
func InternalServerError(error string) (int, interface{}) {
|
|
|
|
return GetErrorResponse(http.StatusInternalServerError, error)
|
2019-12-12 09:35:05 +03:00
|
|
|
}
|