{"record":{"id":"bb206ddfe9ad5744","repo":"GoogleCloudPlatform/microservices-demo","slug":"could-not-retrieve-cart","errorCode":null,"errorMessage":"could not retrieve cart","messagePattern":"could not retrieve cart","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"src/frontend/handlers.go","lineNumber":74,"sourceCode":"\nvar validEnvs = []string{\"local\", \"gcp\", \"azure\", \"aws\", \"onprem\", \"alibaba\"}\n\nfunc (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) {\n\tlog := r.Context().Value(ctxKeyLog{}).(logrus.FieldLogger)\n\tlog.WithField(\"currency\", currentCurrency(r)).Info(\"home\")\n\tcurrencies, err := fe.getCurrencies(r.Context())\n\tif err != nil {\n\t\trenderHTTPError(log, r, w, errors.Wrap(err, \"could not retrieve currencies\"), http.StatusInternalServerError)\n\t\treturn\n\t}\n\tproducts, err := fe.getProducts(r.Context())\n\tif err != nil {\n\t\trenderHTTPError(log, r, w, errors.Wrap(err, \"could not retrieve products\"), http.StatusInternalServerError)\n\t\treturn\n\t}\n\tcart, err := fe.getCart(r.Context(), sessionID(r))\n\tif err != nil {\n\t\trenderHTTPError(log, r, w, errors.Wrap(err, \"could not retrieve cart\"), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\ttype productView struct {\n\t\tItem  *pb.Product\n\t\tPrice *pb.Money\n\t}\n\tps := make([]productView, len(products))\n\tfor i, p := range products {\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, \"failed to do currency conversion for product %s\", p.GetId()), http.StatusInternalServerError)\n\t\t\treturn\n\t\t}\n\t\tps[i] = productView{p, price}\n\t}\n\n\t// Set ENV_PLATFORM (default to local if not set; use env var if set; otherwise detect GCP, which overrides env)_","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/GoogleCloudPlatform/microservices-demo/blob/72ba613a05f7fcee51cf1d0badff401b6ae7074d/src/frontend/handlers.go#L56-L92","documentation":"homeHandler fetches the user's cart via fe.getCart (gRPC CartService.GetCart) using the session id. On error it wraps with errors.Wrap(err, \"could not retrieve cart\") and renders HTTP 500. The home page needs the cart to show item counts, so a cart service failure blocks rendering.","triggerScenarios":"fe.getCart(r.Context(), sessionID(r)) returns error: cart service unavailable, RPC deadline, invalid/empty user/session id passed to GetCart, or cart service returning an error status.","commonSituations":"Cart service (often Redis-backed) down or its Redis unreachable; CART_SERVICE_ADDR wrong; session cookie missing causing empty user id; network policy blocking frontend→cart traffic.","solutions":["Check cart service health and its backing store (e.g. Redis) connectivity","Verify CART_SERVICE_ADDR configuration","Confirm a valid session/user id exists before calling getCart; treat empty session as an empty cart","Inspect the wrapped cause for the gRPC status code (Unavailable vs InvalidArgument)","Consider degrading gracefully (render page with empty cart) instead of 500"],"exampleFix":"// before\ncart, err := fe.getCart(r.Context(), sessionID(r))\nif err != nil {\n    renderHTTPError(log, r, w, errors.Wrap(err, \"could not retrieve cart\"), http.StatusInternalServerError)\n    return\n}\n// after\ncart, err := fe.getCart(r.Context(), sessionID(r))\nif err != nil {\n    log.WithError(err).Warn(\"could not retrieve cart, showing empty cart\")\n    cart = &pb.Cart{}\n}","handlingStrategy":"fallback","validationCode":"userID := sessionID(r)\nif userID == \"\" {\n    cart = &pb.Cart{}\n    // skip GetCart entirely\n}","typeGuard":null,"tryCatchPattern":"cart, err := fe.getCart(r.Context(), sessionID(r))\nif err != nil {\n    log.WithError(err).Warn(\"cart unavailable, rendering empty cart\")\n    cart = &pb.Cart{}\n}","preventionTips":["Treat missing session as an empty cart instead of an RPC call","Ensure the cart service's backing store (Redis) is monitored","Add retry-on-Unavailable policy to the cart client","Verify CART_SERVICE_ADDR configuration per environment"],"tags":["grpc","http","frontend","cart","microservices"],"backgroundTag":"grpc-rpc-failed","analyzedSha":"72ba613a05f7fcee51cf1d0badff401b6ae7074d","analyzedAt":"2026-09-02T01:15:09.673Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}