{"record":{"id":"cbfb15a0c0c79e74","repo":"GoogleCloudPlatform/microservices-demo","slug":"failed-to-empty-cart","errorCode":null,"errorMessage":"failed to empty cart","messagePattern":"failed to empty cart","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"src/frontend/handlers.go","lineNumber":244,"sourceCode":"\tif err != nil {\n\t\trenderHTTPError(log, r, w, errors.Wrap(err, \"could not retrieve product\"), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\tif err := fe.insertCart(r.Context(), sessionID(r), p.GetId(), int32(payload.Quantity)); err != nil {\n\t\trenderHTTPError(log, r, w, errors.Wrap(err, \"failed to add to cart\"), http.StatusInternalServerError)\n\t\treturn\n\t}\n\tw.Header().Set(\"location\", baseUrl + \"/cart\")\n\tw.WriteHeader(http.StatusFound)\n}\n\nfunc (fe *frontendServer) emptyCartHandler(w http.ResponseWriter, r *http.Request) {\n\tlog := r.Context().Value(ctxKeyLog{}).(logrus.FieldLogger)\n\tlog.Debug(\"emptying cart\")\n\n\tif err := fe.emptyCart(r.Context(), sessionID(r)); err != nil {\n\t\trenderHTTPError(log, r, w, errors.Wrap(err, \"failed to empty cart\"), http.StatusInternalServerError)\n\t\treturn\n\t}\n\tw.Header().Set(\"location\", baseUrl + \"/\")\n\tw.WriteHeader(http.StatusFound)\n}\n\nfunc (fe *frontendServer) viewCartHandler(w http.ResponseWriter, r *http.Request) {\n\tlog := r.Context().Value(ctxKeyLog{}).(logrus.FieldLogger)\n\tlog.Debug(\"view user cart\")\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\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","sourceCodeStart":226,"sourceCodeEnd":262,"githubUrl":"https://github.com/GoogleCloudPlatform/microservices-demo/blob/72ba613a05f7fcee51cf1d0badff401b6ae7074d/src/frontend/handlers.go#L226-L262","documentation":"emptyCartHandler failed to empty the user's cart via fe.emptyCart (cartservice EmptyCart RPC). The error is wrapped as \"failed to empty cart\" and returned as HTTP 500 instead of redirecting to the home page.","triggerScenarios":"POST /cart/empty when cartservice EmptyCart returns an error: service unavailable, timeout, or an invalid/empty session user id rejected by the cart service.","commonSituations":"cartservice down or restarting, redis backend unreachable, session/user id missing or malformed (empty session cookie leading to an invalid empty-cart request).","solutions":["Check cartservice health and logs.","Verify CART_SERVICE_ADDR env configuration in frontend.","Check the redis backend used by cartservice.","Ensure sessionID(r) produces a valid non-empty user id; otherwise fix session cookie handling."],"exampleFix":"// before\nif err := fe.emptyCart(r.Context(), sessionID(r)); err != nil {\n// after\nif sessionID(r) == \"\" {\n    http.Redirect(w, r, baseUrl+\"/\", http.StatusFound)\n    return\n}\nif err := fe.emptyCart(r.Context(), sessionID(r)); err != nil {","handlingStrategy":"validation","validationCode":"uid := sessionID(r)\nif uid == \"\" {\n    http.Redirect(w, r, baseUrl+\"/\", http.StatusFound)\n    return\n}","typeGuard":"func hasSession(r *http.Request) bool { return sessionID(r) != \"\" }","tryCatchPattern":"if err := fe.emptyCart(r.Context(), sessionID(r)); err != nil {\n    log.WithField(\"error\", err).Warn(\"failed to empty cart\")\n    renderHTTPError(log, r, w, errors.Wrap(err, \"failed to empty cart\"), http.StatusInternalServerError)\n    return\n}","preventionTips":["Ensure session cookie middleware runs before handlers.","Verify CART_SERVICE_ADDR at frontend startup.","Watch cartservice/redis health; emptying depends on both.","Treat empty-session requests as no-ops, not RPC failures."],"tags":["grpc","http-500","cart","microservices"],"backgroundTag":"rpc-service-unavailable","analyzedSha":"72ba613a05f7fcee51cf1d0badff401b6ae7074d","analyzedAt":"2026-09-02T01:15:09.673Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}