feat: 完善api错误提示
This commit is contained in:
@@ -2,47 +2,38 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
app_errors "gpt-load/internal/errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Response defines the standard JSON response structure.
|
||||
type Response struct {
|
||||
// SuccessResponse defines the standard JSON success response structure.
|
||||
type SuccessResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// ErrorResponse defines the standard JSON error response structure.
|
||||
type ErrorResponse struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// Success sends a standardized success response.
|
||||
func Success(c *gin.Context, data interface{}) {
|
||||
c.JSON(http.StatusOK, Response{
|
||||
c.JSON(http.StatusOK, SuccessResponse{
|
||||
Code: 0,
|
||||
Message: "Success",
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
// Error sends a standardized error response.
|
||||
func Error(c *gin.Context, code int, message string) {
|
||||
c.JSON(code, Response{
|
||||
Code: code,
|
||||
Message: message,
|
||||
Data: nil,
|
||||
// Error sends a standardized error response using an APIError.
|
||||
func Error(c *gin.Context, apiErr *app_errors.APIError) {
|
||||
c.JSON(apiErr.HTTPStatus, ErrorResponse{
|
||||
Code: apiErr.Code,
|
||||
Message: apiErr.Message,
|
||||
})
|
||||
}
|
||||
|
||||
// BadRequest sends a 400 Bad Request error response.
|
||||
func BadRequest(c *gin.Context, message string) {
|
||||
Error(c, http.StatusBadRequest, message)
|
||||
}
|
||||
|
||||
// NotFound sends a 404 Not Found error response.
|
||||
func NotFound(c *gin.Context, message string) {
|
||||
Error(c, http.StatusNotFound, message)
|
||||
}
|
||||
|
||||
// InternalError sends a 500 Internal Server Error response.
|
||||
func InternalError(c *gin.Context, message string) {
|
||||
Error(c, http.StatusInternalServerError, message)
|
||||
}
|
Reference in New Issue
Block a user