forked from baron/baron-sso
45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
package response
|
|
|
|
import "github.com/gofiber/fiber/v2"
|
|
|
|
type ErrorResponse struct {
|
|
Error string `json:"error"`
|
|
Code string `json:"code,omitempty"`
|
|
Details interface{} `json:"details,omitempty"`
|
|
}
|
|
|
|
func Error(c *fiber.Ctx, status int, code, message string) error {
|
|
return ErrorWithDetails(c, status, code, message, nil)
|
|
}
|
|
|
|
func ErrorWithDetails(c *fiber.Ctx, status int, code, message string, details interface{}) error {
|
|
resp := ErrorResponse{
|
|
Error: message,
|
|
Code: code,
|
|
Details: details,
|
|
}
|
|
|
|
return c.Status(status).JSON(resp)
|
|
}
|
|
|
|
func StatusCode(status int) string {
|
|
switch status {
|
|
case fiber.StatusBadRequest:
|
|
return "bad_request"
|
|
case fiber.StatusUnauthorized:
|
|
return "invalid_session"
|
|
case fiber.StatusForbidden:
|
|
return "forbidden"
|
|
case fiber.StatusNotFound:
|
|
return "not_found"
|
|
case fiber.StatusConflict:
|
|
return "conflict"
|
|
case fiber.StatusTooManyRequests:
|
|
return "rate_limited"
|
|
case fiber.StatusServiceUnavailable:
|
|
return "service_unavailable"
|
|
default:
|
|
return "internal_error"
|
|
}
|
|
}
|