Learn how to handle errors in the Notch Pay API
{ "code": 400, "status": "Bad Request", "message": "Error message describing what went wrong", "errors": { "field_name": ["Error description for this field"] } }
Always Check for Errors
// Example in JavaScript if (!response.ok) { const error = await response.json(); // Handle error console.error(`Error: ${error.message}`); }
Handle Different Error Types
// Example in JavaScript if (error.code === 401) { // Handle authentication errors redirectToLogin(); } else if (error.code === 422) { // Handle validation errors showValidationErrors(error.errors); } else if (error.code === 429) { // Handle rate limiting retryWithBackoff(request); }
Implement Retry Logic
// Simplified example async function makeRequestWithRetry(url, options, maxRetries = 3) { let retries = 0; while (retries < maxRetries) { try { const response = await fetch(url, options); if (response.status === 429) { // Rate limited, wait and retry const retryAfter = Math.pow(2, retries); await new Promise(resolve => setTimeout(resolve, retryAfter * 1000)); retries++; continue; } return await response.json(); } catch (error) { if (retries === maxRetries - 1 || error.code < 500) { throw error; // Don't retry client errors or if max retries reached } const delay = Math.pow(2, retries); await new Promise(resolve => setTimeout(resolve, delay * 1000)); retries++; } } }
Provide User-Friendly Messages
// Map API errors to user-friendly messages const errorMessages = { 'insufficient_funds': 'Your card has insufficient funds. Please use a different card.', }; // Display appropriate message const userMessage = errorMessages[error.message] || 'An error occurred. Please try again.';
Was this page helpful?