"frontend/vscode:/vscode.git/clone" did not exist on "0170d19fa7d9fdb5467dfbecb2dcef3372423066"
Commit d3127b8e authored by erio's avatar erio
Browse files

refactor: use structured error responses in channel handler

Replace response.BadRequest with response.ErrorFrom + infraerrors.BadRequest
to provide machine-readable reason codes (VALIDATION_ERROR, INVALID_CHANNEL_ID,
MISSING_PARAMETER) for frontend i18n support.
parent 6de1d0cb
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"strconv" "strconv"
"strings" "strings"
infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors"
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination" "github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
"github.com/Wei-Shaw/sub2api/internal/pkg/response" "github.com/Wei-Shaw/sub2api/internal/pkg/response"
"github.com/Wei-Shaw/sub2api/internal/service" "github.com/Wei-Shaw/sub2api/internal/service"
...@@ -311,7 +312,7 @@ func (h *ChannelHandler) List(c *gin.Context) { ...@@ -311,7 +312,7 @@ func (h *ChannelHandler) List(c *gin.Context) {
func (h *ChannelHandler) GetByID(c *gin.Context) { func (h *ChannelHandler) GetByID(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64) id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil { if err != nil {
response.BadRequest(c, "Invalid channel ID") response.ErrorFrom(c, infraerrors.BadRequest("INVALID_CHANNEL_ID", "Invalid channel ID"))
return return
} }
...@@ -329,13 +330,13 @@ func (h *ChannelHandler) GetByID(c *gin.Context) { ...@@ -329,13 +330,13 @@ func (h *ChannelHandler) GetByID(c *gin.Context) {
func (h *ChannelHandler) Create(c *gin.Context) { func (h *ChannelHandler) Create(c *gin.Context) {
var req createChannelRequest var req createChannelRequest
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "Invalid request: "+err.Error()) response.ErrorFrom(c, infraerrors.BadRequest("VALIDATION_ERROR", err.Error()))
return return
} }
pricing := pricingRequestToService(req.ModelPricing) pricing := pricingRequestToService(req.ModelPricing)
if err := validatePricingBillingMode(pricing); err != nil { if err := validatePricingBillingMode(pricing); err != nil {
response.BadRequest(c, err.Error()) response.ErrorFrom(c, infraerrors.BadRequest("VALIDATION_ERROR", err.Error()))
return return
} }
...@@ -361,13 +362,13 @@ func (h *ChannelHandler) Create(c *gin.Context) { ...@@ -361,13 +362,13 @@ func (h *ChannelHandler) Create(c *gin.Context) {
func (h *ChannelHandler) Update(c *gin.Context) { func (h *ChannelHandler) Update(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64) id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil { if err != nil {
response.BadRequest(c, "Invalid channel ID") response.ErrorFrom(c, infraerrors.BadRequest("INVALID_CHANNEL_ID", "Invalid channel ID"))
return return
} }
var req updateChannelRequest var req updateChannelRequest
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "Invalid request: "+err.Error()) response.ErrorFrom(c, infraerrors.BadRequest("VALIDATION_ERROR", err.Error()))
return return
} }
...@@ -383,7 +384,7 @@ func (h *ChannelHandler) Update(c *gin.Context) { ...@@ -383,7 +384,7 @@ func (h *ChannelHandler) Update(c *gin.Context) {
if req.ModelPricing != nil { if req.ModelPricing != nil {
pricing := pricingRequestToService(*req.ModelPricing) pricing := pricingRequestToService(*req.ModelPricing)
if err := validatePricingBillingMode(pricing); err != nil { if err := validatePricingBillingMode(pricing); err != nil {
response.BadRequest(c, err.Error()) response.ErrorFrom(c, infraerrors.BadRequest("VALIDATION_ERROR", err.Error()))
return return
} }
input.ModelPricing = &pricing input.ModelPricing = &pricing
...@@ -403,7 +404,7 @@ func (h *ChannelHandler) Update(c *gin.Context) { ...@@ -403,7 +404,7 @@ func (h *ChannelHandler) Update(c *gin.Context) {
func (h *ChannelHandler) Delete(c *gin.Context) { func (h *ChannelHandler) Delete(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64) id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil { if err != nil {
response.BadRequest(c, "Invalid channel ID") response.ErrorFrom(c, infraerrors.BadRequest("INVALID_CHANNEL_ID", "Invalid channel ID"))
return return
} }
...@@ -420,7 +421,8 @@ func (h *ChannelHandler) Delete(c *gin.Context) { ...@@ -420,7 +421,8 @@ func (h *ChannelHandler) Delete(c *gin.Context) {
func (h *ChannelHandler) GetModelDefaultPricing(c *gin.Context) { func (h *ChannelHandler) GetModelDefaultPricing(c *gin.Context) {
model := strings.TrimSpace(c.Query("model")) model := strings.TrimSpace(c.Query("model"))
if model == "" { if model == "" {
response.BadRequest(c, "model parameter is required") response.ErrorFrom(c, infraerrors.BadRequest("MISSING_PARAMETER", "model parameter is required").
WithMetadata(map[string]string{"param": "model"}))
return return
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment