Error Handling & Debugging

Forsta HX Platform - API Scripting Guide

Error Handling & Debugging

Try-Catch Wrapper:

/**
 * safeOperation - Demonstrates safe error handling with try-catch
 * @description Wraps potentially risky operations in a try-catch block
 *              to prevent script crashes. If an error occurs, it logs
 *              the error to console and can execute fallback behavior.
 */
function safeOperation() {
    try {
        // Attempt to get the value - may fail if question doesn't exist
        var value = f(AQ1).val();
        // Your logic here - protected by the try block
    } catch(error) {
        // If any error occurs above, execution jumps here
        console.error('Error in AQ1:', error);
        // Fallback behavior - continue gracefully
    }
}

/**
 * Global error handler - catches unhandled errors across the entire page
 * @description Attaches to window.onerror to log any uncaught errors.
 *              Useful for debugging and monitoring in production.
 */
// Global error handler
window.addEventListener('error', function(e) {
    console.error('Survey Error:', e.message);
});

Debug Helper Functions:

/**
 * questionExists - Checks if a question element exists in the DOM
 * @param {string} qid - The question ID to check (e.g., 'AQ1')
 * @returns {boolean} - Returns true if question exists, false otherwise
 * @description Uses jQuery length property to determine if the selector
 *              found any matching elements. Useful for conditional logic.
 */
function questionExists(qid) {
    // f(qid).length returns the number of matching elements
    // If > 0, the question exists in the DOM
    return f(qid).length > 0;
}

/**
 * debugAllQuestions - Logs current values of specified questions to console
 * @description Iterates through a predefined list of question IDs,
 *              checks if each exists, and logs its current value.
 *              Useful for debugging survey state during development.
 */
function debugAllQuestions() {
    ['AQ1', 'AQ2', 'BQ1', 'BQ2'].forEach(function(qid) {
        if(questionExists(qid)) {
            console.log(qid + ':', f(qid).val());
        }
    });
}

// Validate question before using
if(questionExists('AQ1')) {
    var val = f(AQ1).val();
} else {
    console.warn('AQ1 not found');
}

ℹ️ Data Submission Note:

The Forsta HX Platform handles all data submission automatically. Custom AJAX submission is not needed and not recommended for the Euro SaaS environment. The platform includes built-in error handling and retry logic.