{"record":{"id":"47bbbed9402944f6","repo":"GoogleCloudPlatform/microservices-demo","slug":"could-not-convert-currency-for-product-s","errorCode":null,"errorMessage":"could not convert currency for product #%s","messagePattern":"could not convert currency for product #(.+?)","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"src/frontend/handlers.go","lineNumber":292,"sourceCode":"\t\treturn\n\t}\n\n\ttype cartItemView struct {\n\t\tItem     *pb.Product\n\t\tQuantity int32\n\t\tPrice    *pb.Money\n\t}\n\titems := make([]cartItemView, len(cart))\n\ttotalPrice := pb.Money{CurrencyCode: currentCurrency(r)}\n\tfor i, item := range cart {\n\t\tp, err := fe.getProduct(r.Context(), item.GetProductId())\n\t\tif err != nil {\n\t\t\trenderHTTPError(log, r, w, errors.Wrapf(err, \"could not retrieve product #%s\", item.GetProductId()), http.StatusInternalServerError)\n\t\t\treturn\n\t\t}\n\t\tprice, err := fe.convertCurrency(r.Context(), p.GetPriceUsd(), currentCurrency(r))\n\t\tif err != nil {\n\t\t\trenderHTTPError(log, r, w, errors.Wrapf(err, \"could not convert currency for product #%s\", item.GetProductId()), http.StatusInternalServerError)\n\t\t\treturn\n\t\t}\n\n\t\tmultPrice := money.MultiplySlow(*price, uint32(item.GetQuantity()))\n\t\titems[i] = cartItemView{\n\t\t\tItem:     p,\n\t\t\tQuantity: item.GetQuantity(),\n\t\t\tPrice:    &multPrice}\n\t\ttotalPrice = money.Must(money.Sum(totalPrice, multPrice))\n\t}\n\ttotalPrice = money.Must(money.Sum(totalPrice, *shippingCost))\n\tyear := time.Now().Year()\n\n\tif err := templates.ExecuteTemplate(w, \"cart\", injectCommonTemplateData(r, map[string]interface{}{\n\t\t\"currencies\":       currencies,\n\t\t\"recommendations\":  recommendations,\n\t\t\"cart_size\":        cartSize(cart),\n\t\t\"shipping_cost\":    shippingCost,","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/GoogleCloudPlatform/microservices-demo/blob/72ba613a05f7fcee51cf1d0badff401b6ae7074d/src/frontend/handlers.go#L274-L310","documentation":"While building the cart item views, viewCartHandler failed to convert a product's USD price to the selected currency via fe.convertCurrency. The error uses errors.Wrapf with the product ID and is rendered as HTTP 500, aborting the cart page render.","triggerScenarios":"GET /cart when currencyservice Convert fails for any cart item's price: currencyservice down, unsupported currency code from the user's cookie, or context canceled mid-loop.","commonSituations":"currencyservice outage, invalid/stale currency cookie value, per-request deadline exhausted because conversion fails on an early item in the loop.","solutions":["Check currencyservice health and logs.","Validate currentCurrency against supported currencies before the loop.","Convert all prices in a single batch or cache conversions per request.","Fall back to USD price with a warning instead of returning HTTP 500."],"exampleFix":"// before\nprice, err := fe.convertCurrency(r.Context(), p.GetPriceUsd(), currentCurrency(r))\nif err != nil {\n    renderHTTPError(log, r, w, errors.Wrapf(err, \"could not convert currency for product #%s\", item.GetProductId()), http.StatusInternalServerError)\n// after\nprice, err := fe.convertCurrency(r.Context(), p.GetPriceUsd(), currentCurrency(r))\nif err != nil {\n    log.WithField(\"product\", item.GetProductId()).WithField(\"error\", err).Warn(\"currency conversion failed, using USD\")\n    usdPrice := p.GetPriceUsd()\n    price = &usdPrice","handlingStrategy":"fallback","validationCode":"if !supportedCurrency(currentCurrency(r), supportedCurrencies) {\n    log.Warn(\"unsupported currency in cookie; will fall back to USD\")\n}","typeGuard":"func supportedCurrency(code string, list []*pb.Currency) bool {\n    for _, c := range list { if code == c.Code { return true } }\n    return false\n}","tryCatchPattern":"price, err := fe.convertCurrency(r.Context(), p.GetPriceUsd(), currentCurrency(r))\nif err != nil {\n    log.WithField(\"product\", item.GetProductId()).WithField(\"error\", err).Warn(\"conversion failed, using USD\")\n    usd := p.GetPriceUsd(); price = &usd\n}","preventionTips":["Fall back to USD pricing on conversion failure.","Validate currency cookies against the supported list.","Cache conversions per request to avoid repeated failing RPCs.","Alert on currencyservice Convert error rates."],"tags":["grpc","http-500","currency","cart"],"backgroundTag":"rpc-service-unavailable","analyzedSha":"72ba613a05f7fcee51cf1d0badff401b6ae7074d","analyzedAt":"2026-09-02T01:15:09.673Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}