de30497f59
- Implemented tenant and user management features including: - Tenant creation and management with associated migrations. - User creation and management with associated migrations. - Tenant membership management with associated migrations. - Platform user roles management with associated migrations. - Quota management with associated migrations. - Article and template management with associated migrations. - Added HTTP handlers for templates and workspaces. - Created tests for protected and public routes. - Introduced a script to check tenant scope in SQL queries. - Documented task plan for backend completion and frontend foundation.
71 lines
1.3 KiB
Go
71 lines
1.3 KiB
Go
package response
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AppError struct {
|
|
HTTPStatus int `json:"-"`
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Detail string `json:"detail,omitempty"`
|
|
Cause error `json:"-"`
|
|
}
|
|
|
|
func (e *AppError) Error() string {
|
|
if e.Cause != nil {
|
|
return e.Cause.Error()
|
|
}
|
|
return e.Message
|
|
}
|
|
|
|
func Success(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 0,
|
|
"message": "ok",
|
|
"data": data,
|
|
"request_id": getRequestID(c),
|
|
})
|
|
}
|
|
|
|
func SuccessWithStatus(c *gin.Context, status int, data interface{}) {
|
|
c.JSON(status, gin.H{
|
|
"code": 0,
|
|
"message": "ok",
|
|
"data": data,
|
|
"request_id": getRequestID(c),
|
|
})
|
|
}
|
|
|
|
func Error(c *gin.Context, err error) {
|
|
appErr := Normalize(err)
|
|
c.JSON(appErr.HTTPStatus, gin.H{
|
|
"code": appErr.Code,
|
|
"message": appErr.Message,
|
|
"detail": appErr.Detail,
|
|
"request_id": getRequestID(c),
|
|
})
|
|
}
|
|
|
|
func Normalize(err error) *AppError {
|
|
if appErr, ok := err.(*AppError); ok {
|
|
return appErr
|
|
}
|
|
return &AppError{
|
|
HTTPStatus: http.StatusInternalServerError,
|
|
Code: 50000,
|
|
Message: "internal_error",
|
|
Detail: err.Error(),
|
|
Cause: err,
|
|
}
|
|
}
|
|
|
|
func getRequestID(c *gin.Context) string {
|
|
if rid, ok := c.Get("request_id"); ok {
|
|
return rid.(string)
|
|
}
|
|
return ""
|
|
}
|