Clean public release with: - Go/Gin/PostgreSQL/Redis architecture - Multi-tenant support - JWT authentication - Docker Compose deployment - Environment-based configuration
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package response
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"mengstack/internal/kernel/errors"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Response struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data interface{} `json:"data,omitempty"`
|
|
TraceID string `json:"trace_id"`
|
|
}
|
|
|
|
func Success(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusOK, Response{
|
|
Code: 0,
|
|
Message: "success",
|
|
Data: data,
|
|
TraceID: c.GetString("trace_id"),
|
|
})
|
|
}
|
|
|
|
func Fail(c *gin.Context, err *errors.AppError) {
|
|
c.JSON(err.HTTPCode, Response{
|
|
Code: -1,
|
|
Message: err.Message,
|
|
TraceID: c.GetString("trace_id"),
|
|
})
|
|
}
|
|
|
|
func FailWithMessage(c *gin.Context, err *errors.AppError, message string) {
|
|
c.JSON(err.HTTPCode, Response{
|
|
Code: -1,
|
|
Message: message,
|
|
TraceID: c.GetString("trace_id"),
|
|
})
|
|
}
|
|
|
|
func HandleError(c *gin.Context, err error) {
|
|
if appErr, ok := errors.IsAppError(err); ok {
|
|
Fail(c, appErr)
|
|
return
|
|
}
|
|
Fail(c, errors.ErrInternal)
|
|
}
|
|
|
|
func NewBadRequest(msg string) *errors.AppError {
|
|
return errors.New("BAD_REQUEST", msg, http.StatusBadRequest)
|
|
}
|