Home > widget-integration > CartService > handleCartError

CartService.handleCartError() method

Handle cart operation errors with custom logging and user feedback Override to customize error handling, user notifications, and tracking

Common customization scenarios: - Show user-friendly error messages - Track errors in analytics - Send error notifications to support team - Implement retry logic for transient errors - Trigger fallback behaviors

Signature:

protected handleCartError(error: string, errorType: string, context?: unknown): AddToCartResult;

Parameters

Parameter

Type

Description

error

string

Error message describing what went wrong

errorType

string

Error type identifier for categorization and tracking

context

unknown

(Optional) Additional context about the failed operation (cart item, request details)

Returns:

AddToCartResult

AddToCartResult object with success=false and error message

Example

Custom error tracking and notifications

protected handleCartError(
  error: string,
  errorType: string,
  context?: unknown
): AddToCartResult {
  // Log to external error tracking service
  this.trackError({
    type: errorType,
    message: error,
    context,
    timestamp: Date.now(),
  });

  // Show user-friendly notification
  this.showErrorNotification(this.getUserFriendlyMessage(errorType));

  // Return structured error
  return super.handleCartError(error, errorType, context);
}

private getUserFriendlyMessage(errorType: string): string {
  const messages = {
    'add_to_cart_failed': 'Unable to add item to cart. Please try again.',
    'inventory_unavailable': 'This item is currently out of stock.',
    'invalid_quantity': 'Please enter a valid quantity.',
  };
  return messages[errorType] || 'An error occurred. Please try again.';
}