Error Propagation

Propagate errors through call stack

IntermediateTopic: Error Handling
Back

JavaScript Error Propagation Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
// Method 1: Re-throw error
function level1() {
    try {
        level2();
    } catch (error) {
        console.error('Level 1 caught:', error.message);
        throw error; // Re-throw
    }
}

function level2() {
    try {
        level3();
    } catch (error) {
        console.error('Level 2 caught:', error.message);
        throw error; // Re-throw
    }
}

function level3() {
    throw new Error('Error from level 3');
}

try {
    level1();
} catch (error) {
    console.error('Final catch:', error.message);
}

// Method 2: Wrap error
function processData(data) {
    try {
        return JSON.parse(data);
    } catch (error) {
        throw new Error(`Failed to process data: ${error.message}`);
    }
}

try {
    processData('invalid');
} catch (error) {
    console.error('Wrapped error:', error.message);
}

// Method 3: Error chain
class ChainedError extends Error {
    constructor(message, cause) {
        super(message);
        this.cause = cause;
        this.name = 'ChainedError';
    }
}

function operation1() {
    try {
        operation2();
    } catch (error) {
        throw new ChainedError('Operation 1 failed', error);
    }
}

function operation2() {
    throw new Error('Operation 2 failed');
}

try {
    operation1();
} catch (error) {
    console.error('Chained error:', error.message);
    console.error('Caused by:', error.cause.message);
}

// Method 4: Error handling middleware
function errorHandler(error, context) {
    console.error('Error in', context, ':', error.message);
    // Log to service, send notification, etc.
}

function riskyOperation() {
    try {
        throw new Error('Operation failed');
    } catch (error) {
        errorHandler(error, 'riskyOperation');
        throw error;
    }
}
Output
Level 3 caught: Error from level 3
Level 2 caught: Error from level 3
Level 1 caught: Error from level 3
Final catch: Error from level 3
Wrapped error: Failed to process data: Unexpected token i in JSON at position 0
Chained error: Operation 1 failed
Caused by: Operation 2 failed
Error in riskyOperation : Operation failed

Understanding Error Propagation

Error propagation moves errors up call stack.

Re-throwing

Catch and re-throw
Preserve original error
Add context if needed

Wrapping Errors

Wrap in new error
Add context
Preserve original

Error Chains

Link errors
Track cause
Full error history

Error Middleware

Centralized handling
Logging
Notifications

Best Practices

Re-throw when appropriate
Add context
Don't swallow errors
Log properly

Let us now understand every line and the components of the above program.

Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.

Table of Contents