45 lines
1010 B
Go
45 lines
1010 B
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ErrorBody struct {
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
Details any `json:"details,omitempty"`
|
|
RequestID string `json:"request_id,omitempty"`
|
|
}
|
|
|
|
func requestID(c *gin.Context) string {
|
|
if v, ok := c.Get("request_id"); ok {
|
|
if s, ok := v.(string); ok {
|
|
return s
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func JSONError(c *gin.Context, status int, code, message string, details any) {
|
|
c.JSON(status, ErrorBody{
|
|
Code: code,
|
|
Message: message,
|
|
Details: details,
|
|
RequestID: requestID(c),
|
|
})
|
|
}
|
|
|
|
func JSONBadRequest(c *gin.Context, code, message string, details any) {
|
|
JSONError(c, http.StatusBadRequest, code, message, details)
|
|
}
|
|
|
|
func JSONUnauthorized(c *gin.Context, code, message string) {
|
|
JSONError(c, http.StatusUnauthorized, code, message, nil)
|
|
}
|
|
|
|
func JSONInternal(c *gin.Context, message string, details any) {
|
|
JSONError(c, http.StatusInternalServerError, "INTERNAL_ERROR", message, details)
|
|
}
|