package server import ( "errors" "strings" "google.golang.org/genproto/googleapis/rpc/errdetails" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/repository" ) // productCreateError preserves the existing gRPC code and message, while // attaching the field that failed validation for callers that understand the // standard google.rpc.BadRequest detail. func productCreateError(code codes.Code, message, field string) error { if field == "" { return status.Error(code, message) } grpcStatus := status.New(code, message) withDetails, err := grpcStatus.WithDetails(&errdetails.BadRequest{ FieldViolations: []*errdetails.BadRequest_FieldViolation{{ Field: field, Description: message, }}, }) if err != nil { return grpcStatus.Err() } return withDetails.Err() } func productMasterReferenceField(err error) string { switch { case errors.Is(err, repository.ErrBrandNotFound), errors.Is(err, repository.ErrBrandInactive), errors.Is(err, repository.ErrBrandSupplierMismatch): return "brand_id" case errors.Is(err, repository.ErrSupplierNotFound), errors.Is(err, repository.ErrSupplierInactive): return "supplier_id" default: return "" } } func productCreateFieldFromMessage(message string) string { if strings.HasPrefix(message, "skus[") { separator := strings.Index(message, ":") if separator > 0 { prefix := message[:separator] detail := strings.TrimSpace(message[separator+1:]) for _, field := range []string{"supply_price", "weight_kg", "stock_quantity", "price", "status", "k1", "v1", "k2", "v2", "k3", "v3"} { if strings.HasPrefix(detail, field) || strings.Contains(detail, field+"/") { return prefix + "." + field } } return prefix } } if message == "request is required" { return "$" } if message == "unsupported product status" { return "status" } if strings.HasPrefix(message, "on-sale product") { return "skus" } for _, field := range []string{ "title", "detail_html", "leaf_catalog_category_ids", "group_ids", "product_category_ids", "freight_fixed_amount", "goods_type", "freight_charge_type", "freight_template_id", "sale_time_type", "scheduled_sale_time", "presale_type", "purchase_limit_quantity", "delivery_time_type", "delivery_start_at", "delivery_after_paid_days", "cross_border_info", } { if strings.HasPrefix(message, field) { return field } } return "" }