Last active
June 4, 2025 09:07
-
-
Save acal/5575d3452ceb05b855847735e5278754 to your computer and use it in GitHub Desktop.
Update to Angelleye Checkout Error Handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
handleCreateOrderError: (error, errorLogId) => { | |
console.log('create_order_error', error, angelleyeOrder.lastApiResponse); | |
angelleyeOrder.hideProcessingSpinner(); | |
jQuery(document.body).trigger('angelleye_paypal_onerror'); | |
let errorMessage = ''; | |
// 1. If error is an object with a data.messages array (eg. AJAX response) | |
if (error && error.data && Array.isArray(error.data.messages) && error.data.messages.length > 0) { | |
errorMessage = error.data.messages.map(msg => `<li>${msg}</li>`).join(''); | |
} | |
// 2. If error is an object with a message property | |
else if (error && error.message) { | |
// If the error is the PayPal SDK "Expected an order id to be passed" | |
if (error.message.indexOf('Expected an order id to be passed') !== -1 && angelleyeOrder.lastApiResponse) { | |
// Try to extract WooCommerce error from lastApiResponse | |
try { | |
const parsed = JSON.parse(angelleyeOrder.lastApiResponse); | |
if (parsed && parsed.data && Array.isArray(parsed.data.messages) && parsed.data.messages.length > 0) { | |
errorMessage = parsed.data.messages.map(msg => `<li>${msg}</li>`).join(''); | |
} else if (parsed && parsed.data && typeof parsed.data === 'string') { | |
errorMessage = `<li>${parsed.data}</li>`; | |
} else { | |
errorMessage = `<li>${localizedMessages.create_order_error}</li>`; | |
} | |
} catch (e) { | |
// If lastApiResponse is not JSON, just show it as HTML/text if it's short | |
if (angelleyeOrder.lastApiResponse && angelleyeOrder.lastApiResponse.length < 500) { | |
errorMessage = angelleyeOrder.lastApiResponse; | |
} else { | |
errorMessage = `<li>${localizedMessages.create_order_error}</li>`; | |
} | |
} | |
} else { | |
errorMessage = error.message.includes('<li>') ? error.message : `<li>${error.message}</li>`; | |
} | |
} | |
// 3. If error is a string | |
else if (typeof error === 'string') { | |
errorMessage = error.includes('<li>') ? error : `<li>${error}</li>`; | |
} | |
// 4. If error is an object with a responseText property | |
else if (error && error.responseText) { | |
errorMessage = error.responseText; | |
} | |
// Only fallback to generic if errorMessage is empty or just <li></li> | |
if (!errorMessage || errorMessage === '<li></li>') { | |
if (angelleyeOrder.lastApiResponse && angelleyeOrder.lastApiResponse.length < 500) { | |
errorMessage = angelleyeOrder.lastApiResponse; | |
} else { | |
errorMessage = `<li>${localizedMessages.create_order_error}</li>`; | |
} | |
} | |
angelleyeOrder.showError(errorMessage); | |
angelleyeOrder.scrollToWooCommerceNoticesSection(); | |
if (angelleyeOrder.isCheckoutPage() === false) { | |
// window.location.href = window.location.href; | |
} | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment