GoogleCloudPlatform/microservices-demo · error
failed to add to cart
Error message
failed to add to cart
What it means
addToCartHandler failed to insert the item into the cart via fe.insertCart (cartservice InsertItem RPC). The error is wrapped as "failed to add to cart" and rendered as HTTP 500; the redirect to /cart never happens.
Source
Thrown at src/frontend/handlers.go:232
productID := r.FormValue("product_id")
payload := validator.AddToCartPayload{
Quantity: quantity,
ProductID: productID,
}
if err := payload.Validate(); err != nil {
renderHTTPError(log, r, w, validator.ValidationErrorResponse(err), http.StatusUnprocessableEntity)
return
}
log.WithField("product", payload.ProductID).WithField("quantity", payload.Quantity).Debug("adding to cart")
p, err := fe.getProduct(r.Context(), payload.ProductID)
if err != nil {
renderHTTPError(log, r, w, errors.Wrap(err, "could not retrieve product"), http.StatusInternalServerError)
return
}
if err := fe.insertCart(r.Context(), sessionID(r), p.GetId(), int32(payload.Quantity)); err != nil {
renderHTTPError(log, r, w, errors.Wrap(err, "failed to add to cart"), http.StatusInternalServerError)
return
}
w.Header().Set("location", baseUrl + "/cart")
w.WriteHeader(http.StatusFound)
}
func (fe *frontendServer) emptyCartHandler(w http.ResponseWriter, r *http.Request) {
log := r.Context().Value(ctxKeyLog{}).(logrus.FieldLogger)
log.Debug("emptying cart")
if err := fe.emptyCart(r.Context(), sessionID(r)); err != nil {
renderHTTPError(log, r, w, errors.Wrap(err, "failed to empty cart"), http.StatusInternalServerError)
return
}
w.Header().Set("location", baseUrl + "/")
w.WriteHeader(http.StatusFound)
}
View on GitHub (pinned to 72ba613a05)
Solutions
- Check cartservice and its redis backend health.
- Verify CART_SERVICE_ADDR configuration.
- Validate quantity > 0 before calling insertCart.
- Inspect the wrapped root cause in frontend logs via renderHTTPError output.
Example fix
// before
if err := fe.insertCart(r.Context(), sessionID(r), p.GetId(), int32(payload.Quantity)); err != nil {
// after
if payload.Quantity <= 0 {
renderHTTPError(log, r, w, errors.New("invalid quantity"), http.StatusBadRequest)
return
}
if err := fe.insertCart(r.Context(), sessionID(r), p.GetId(), int32(payload.Quantity)); err != nil { Defensive patterns
Strategy: validation
Validate before calling
if payload.Quantity <= 0 {
http.Error(w, "quantity must be positive", http.StatusBadRequest)
return
}
if sessionID(r) == "" {
http.Error(w, "missing session", http.StatusBadRequest)
return
} Type guard
func validAddToCart(qty int32, productID, userID string) bool {
return qty > 0 && productID != "" && userID != ""
} Try / catch
if err := fe.insertCart(r.Context(), sessionID(r), p.GetId(), int32(payload.Quantity)); err != nil {
if status.Code(err) == codes.Unavailable {
log.Warn("cartservice unavailable; retry advised")
}
renderHTTPError(log, r, w, errors.Wrap(err, "failed to add to cart"), http.StatusInternalServerError)
return
} Prevention
- Validate quantity and session before the RPC.
- Retry insertCart with backoff on codes.Unavailable.
- Keep cartservice's redis backend monitored.
- Log the wrapped root cause chain.
When it happens
Trigger: POST /cart when cartservice InsertItem returns an error: cart service down, deadline exceeded, or a quantity<=0 rejected by the cart service's validation.
Common situations: cartservice crash/restart mid-request, redis backend (cart store) unavailable, negative or zero quantity submitted from a tampered form.
Related errors
- failed to empty cart
- failed to get user cart during checkout: %+v
- failed to empty user cart during checkout: %+v
- could not retrieve cart
- failed to convert currency
AI-assisted analysis of GoogleCloudPlatform/microservices-demo@72ba613a05 (2026-09-02).
Data as JSON: /api/errors/a468d2be4a6792c2.
Report an issue: GitHub.