Skip to main content

Error Response Format

When an API request fails, OpenCart returns a JSON response with an error object containing detailed error information.

Standard Error Response

{
  "error": {
    "warning": "General error message describing what went wrong",
    "field_name": "Field-specific error message"
  }
}

Example Error Response

{
  "error": {
    "warning": "Unable to add product to cart",
    "firstname": "First name must be between 1 and 32 characters!",
    "email": "E-Mail address does not appear to be valid!",
    "option_12": "Product option is required!"
  }
}

Error Types

Errors related to input validation, including length checks, format validation, and required fields.
{
  "error": {
    "firstname": "First name must be between 1 and 32 characters!",
    "lastname": "Last name must be between 1 and 32 characters!",
    "email": "E-Mail address does not appear to be valid!",
    "telephone": "Telephone must be between 3 and 32 characters!"
  }
}
Errors related to product availability, stock, and options.
{
  "error": {
    "warning": "Product not found",
    "product_0_product": "Product does not exist!",
    "product_0_option_5": "Size is required!"
  }
}
Errors related to cart operations and stock validation.
{
  "error": {
    "warning": "Products marked with *** are not available in the desired quantity or not in stock!",
    "stock": "Product is out of stock"
  }
}
Errors related to address validation.
{
  "error": {
    "payment_firstname": "First name must be between 1 and 32 characters!",
    "payment_address_1": "Address must be between 3 and 128 characters!",
    "payment_city": "City must be between 2 and 128 characters!",
    "payment_postcode": "Postcode must be between 2 and 10 characters!",
    "payment_country": "Please select a country!",
    "payment_zone": "Please select a region / state!"
  }
}
Errors during order confirmation and processing.
{
  "error": {
    "customer": "Customer data is required!",
    "product": "Cart is empty!",
    "payment_address": "Payment address is required!",
    "shipping_address": "Shipping address is required!",
    "shipping_method": "Shipping method is required!",
    "payment_method": "Payment method is required!"
  }
}

Common Error Messages

Customer & Authentication Errors

Error MessageCauseSolution
Customer data requiredCustomer not set in sessionCall customer API to set customer data
Customer group not foundInvalid customer group IDUse valid customer group ID
Customer does not existInvalid customer IDVerify customer exists in database

Product & Cart Errors

Error MessageCauseSolution
Product does not existInvalid product IDVerify product ID is correct
Product is out of stockInsufficient stock quantityCheck product quantity availability
Minimum order quantity is XBelow minimum quantityIncrease quantity to meet minimum
Product option is requiredMissing required optionProvide all required product options
Product option is not validInvalid option valueUse valid option IDs and values

Address Errors

Error MessageCauseSolution
First name must be between 1 and 32 charactersInvalid firstname lengthProvide valid firstname
Address must be between 3 and 128 charactersInvalid address lengthProvide complete address
Please select a countryMissing or invalid countryProvide valid country_id
Please select a region / stateMissing zone for countryProvide valid zone_id
Postcode must be between 2 and 10 charactersInvalid postcodeProvide valid postcode

Order Errors

Error MessageCauseSolution
Cart is emptyNo products in cartAdd products before checkout
Payment address is requiredMissing payment addressSet payment address
Shipping address is requiredMissing shipping addressSet shipping address
Shipping method is requiredNo shipping method selectedSelect shipping method
Payment method is requiredNo payment method selectedSelect payment method

Validation Functions

OpenCart uses these validation functions:

Length Validation

oc_validate_length($value, $min, $max)
Validates string length is between min and max characters. Example:
if (!oc_validate_length($firstname, 1, 32)) {
    $error['firstname'] = 'First name must be between 1 and 32 characters!';
}

Email Validation

oc_validate_email($email)
Validates email address format. Example:
if (!oc_validate_email($email)) {
    $error['email'] = 'E-Mail address does not appear to be valid!';
}

Regex Validation

oc_validate_regex($value, $pattern)
Validates value against custom regex pattern. Example:
if (!oc_validate_regex($option_value, $validation_pattern)) {
    $error['option_' . $option_id] = sprintf('Invalid format for %s', $option_name);
}

Error Handling Best Practices

Check for Errors

Always check if the response contains an error key before processing data.

Display Field Errors

Show field-specific errors next to the relevant input fields for better UX.

Log API Errors

Log error responses for debugging and monitoring API issues.

Retry Logic

Implement retry logic for transient errors with exponential backoff.

Example Error Handling

PHP Example

function handleApiResponse($response) {
    $data = json_decode($response, true);
    
    if (isset($data['error'])) {
        // General error
        if (isset($data['error']['warning'])) {
            error_log('API Error: ' . $data['error']['warning']);
        }
        
        // Field-specific errors
        foreach ($data['error'] as $field => $message) {
            if ($field !== 'warning') {
                error_log("Field Error [{$field}]: {$message}");
            }
        }
        
        return false;
    }
    
    if (isset($data['success'])) {
        return $data;
    }
    
    return false;
}

JavaScript Example

function handleApiResponse(response) {
  if (response.data.error) {
    const errors = response.data.error;
    
    // Log general error
    if (errors.warning) {
      console.error('API Error:', errors.warning);
    }
    
    // Display field errors
    Object.keys(errors).forEach(field => {
      if (field !== 'warning') {
        const errorElement = document.querySelector(`#error-${field}`);
        if (errorElement) {
          errorElement.textContent = errors[field];
          errorElement.style.display = 'block';
        }
      }
    });
    
    return false;
  }
  
  if (response.data.success) {
    console.log('Success:', response.data.success);
    return response.data;
  }
  
  return false;
}

HTTP Status Codes

OpenCart API typically returns HTTP 200 status code for all responses. Error detection should be based on the presence of the error key in the JSON response, not HTTP status codes.

Debugging Tips

  1. Enable Error Logging - Check OpenCart error logs for detailed error information
  2. Validate Input Data - Ensure all required fields are provided with correct data types
  3. Check Session State - Verify session contains required data before operations
  4. Test Incrementally - Test each API call individually before chaining operations
  5. Review Source Code - Check controller validation logic for specific requirements

Next Steps

Authentication

Learn about API authentication

Cart API

Explore cart operations

Orders API

Learn order management

Products API

Access product information