package controller import ( "net/http" "github.com/gin-gonic/gin" "nl-pms-api/internal/commonservice" ) // writeErr 统一把业务错误写成 {"error":"CODE"}。 func writeErr(c *gin.Context, err error) { if err == nil { return } if ae, ok := commonservice.AsAppError(err); ok { c.JSON(ae.Status, gin.H{"error": ae.Code}) return } c.JSON(http.StatusInternalServerError, gin.H{"error": "INTERNAL"}) } func bindJSON(c *gin.Context, dst any) bool { if err := c.ShouldBindJSON(dst); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "BAD_REQUEST"}) return false } return true }