Error Recovery

Recover from errors gracefully

AdvancedTopic: Error Handling
Back

JavaScript Error Recovery Program

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

Try This Code
// Method 1: Fallback values
function fetchWithFallback(url, fallback) {
    return fetch(url)
        .then(response => response.json())
        .catch(error => {
            console.warn('Fetch failed, using fallback:', error);
            return fallback;
        });
}

// Method 2: Retry with fallback
async function operationWithFallback(primary, fallback, maxRetries = 3) {
    for (let i = 0; i < maxRetries; i++) {
        try {
            return await primary();
        } catch (error) {
            if (i === maxRetries - 1) {
                console.warn('Primary failed, using fallback');
                return await fallback();
            }
            await new Promise(resolve => setTimeout(resolve, 1000));
        }
    }
}

// Method 3: Circuit breaker
class CircuitBreaker {
    constructor(threshold = 5, timeout = 60000) {
        this.failures = 0;
        this.threshold = threshold;
        this.timeout = timeout;
        this.state = 'CLOSED'; // CLOSED, OPEN, HALF_OPEN
        this.nextAttempt = Date.now();
    }
    
    async execute(fn) {
        if (this.state === 'OPEN') {
            if (Date.now() < this.nextAttempt) {
                throw new Error('Circuit breaker is OPEN');
            }
            this.state = 'HALF_OPEN';
        }
        
        try {
            const result = await fn();
            this.onSuccess();
            return result;
        } catch (error) {
            this.onFailure();
            throw error;
        }
    }
    
    onSuccess() {
        this.failures = 0;
        this.state = 'CLOSED';
    }
    
    onFailure() {
        this.failures++;
        if (this.failures >= this.threshold) {
            this.state = 'OPEN';
            this.nextAttempt = Date.now() + this.timeout;
        }
    }
}

const breaker = new CircuitBreaker();
breaker.execute(() => fetch('https://api.example.com/data'));

// Method 4: Graceful degradation
function featureWithFallback(feature, fallback) {
    try {
        return feature();
    } catch (error) {
        console.warn('Feature unavailable, using fallback');
        return fallback();
    }
}

// Method 5: Error recovery strategies
class RecoveryStrategy {
    static async retry(fn, maxRetries) {
        // Retry logic
    }
    
    static async fallback(fn, fallbackFn) {
        try {
            return await fn();
        } catch (error) {
            return await fallbackFn();
        }
    }
    
    static async timeout(fn, timeoutMs, fallback) {
        return Promise.race([
            fn(),
            new Promise((_, reject) => 
                setTimeout(() => reject(new Error('Timeout')), timeoutMs)
            )
        ]).catch(() => fallback());
    }
}
Output
Fetch failed, using fallback: Error
Primary failed, using fallback

Understanding Error Recovery

Error recovery maintains functionality.

Fallback Values

Default data
Cached data
Static content
Graceful degradation

Retry with Fallback

Try primary
Retry if fails
Use fallback
Maintain service

Circuit Breaker

Prevent cascading failures
Open on threshold
Half-open for testing
Close on success

Graceful Degradation

Feature unavailable
Use simpler version
Maintain core functionality
User experience

Recovery Strategies

Retry
Fallback
Timeout
Multiple strategies

Best Practices

Plan for failures
Have fallbacks
Monitor recovery
Test strategies

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.

Practical Learning Notes for Error Recovery

This JavaScript program is part of the "Error Handling" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents