package response import ( "net/http" "time" "github.com/gin-gonic/gin" ) // ErrorResponse represents a structured error response type ErrorResponse struct { Code int `json:"code"` Message string `json:"message"` Status int `json:"status"` Now int64 `json:"now"` } // NewErrorResponse creates a new ErrorResponse func NewErrorResponse(message string, status int, code int) *ErrorResponse { return &ErrorResponse{ Code: code, Message: message, Status: status, Now: time.Now().Unix(), } } // Send sends the error response to the client. // It aborts the request and responds with the error response as JSON. func (sr *ErrorResponse) Send(c *gin.Context) { c.AbortWithStatusJSON(sr.Status, sr) } // BadRequestError represents a 400 Bad Request error func BadRequestError(c *gin.Context, code int, messages ...string) { message := "" if len(messages) > 0 { message = messages[0] } if message == "" { message = GetReasonPhrase(http.StatusBadRequest) } response := NewErrorResponse(message, http.StatusBadRequest, code) response.Send(c) } // NotFoundError represents a 404 Not Found error func NotFoundError(c *gin.Context, code int, messages ...string) { message := "" if len(messages) > 0 { message = messages[0] } if message == "" { message = GetReasonPhrase(http.StatusNotFound) } response := NewErrorResponse(message, http.StatusNotFound, code) response.Send(c) } // TooManyRequestsError represents a 429 Too Many Requests error func TooManyRequestsError(c *gin.Context, code int, messages ...string) { message := "" if len(messages) > 0 { message = messages[0] } if message == "" { message = GetReasonPhrase(http.StatusTooManyRequests) } response := NewErrorResponse(message, http.StatusTooManyRequests, code) response.Send(c) } // UnauthorizedError represents a 401 Unauthorized error func UnauthorizedError(c *gin.Context, code int, messages ...string) { message := "" if len(messages) > 0 { message = messages[0] } if message == "" { message = GetReasonPhrase(http.StatusUnauthorized) } response := NewErrorResponse(message, http.StatusUnauthorized, code) response.Send(c) } // ForbiddenError handles the generation and sending of a Forbidden error response. func ForbiddenError(c *gin.Context, code int, messages ...string) { message := "" if len(messages) > 0 { message = messages[0] } if message == "" { message = GetReasonPhrase(http.StatusForbidden) } response := NewErrorResponse(message, http.StatusForbidden, code) response.Send(c) } // ConflictError represents a 409 Conflict error func ConflictError(c *gin.Context, code int, messages ...string) { message := "" if len(messages) > 0 { message = messages[0] } if message == "" { message = GetReasonPhrase(http.StatusConflict) } response := NewErrorResponse(message, http.StatusConflict, code) response.Send(c) } // EntityTooLargeError handles the HTTP 413 Request Entity Too Large error. func EntityTooLargeError(c *gin.Context, code int, messages ...string) { message := "" if len(messages) > 0 { message = messages[0] } if message == "" { message = GetReasonPhrase(http.StatusRequestEntityTooLarge) } response := NewErrorResponse(message, http.StatusRequestEntityTooLarge, code) response.Send(c) } // UnSupportMediaTypeError handles the unsupported media type error by sending an error response to the client. func UnSupportMediaTypeError(c *gin.Context, code int, messages ...string) { message := "" if len(messages) > 0 { message = messages[0] } if message == "" { message = GetReasonPhrase(http.StatusUnsupportedMediaType) } response := NewErrorResponse(message, http.StatusUnsupportedMediaType, code) response.Send(c) } // InternalServerError represents a 500 Internal Server Error func InternalServerError(c *gin.Context, code int, messages ...string) { message := "" if len(messages) > 0 { message = messages[0] } if message == "" { message = GetReasonPhrase(http.StatusInternalServerError) } response := NewErrorResponse(message, http.StatusInternalServerError, code) response.Send(c) } // ServiceUnavailable represents a 503 Service Unavailable func ServiceUnavailable(c *gin.Context, code int, messages ...string) { message := "" if len(messages) > 0 { message = messages[0] } if message == "" { message = GetReasonPhrase(http.StatusServiceUnavailable) } response := NewErrorResponse(message, http.StatusServiceUnavailable, code) response.Send(c) }