Skip to content

JavaScript Proxy Handler Trap Error Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about JavaScript Proxy Handler Trap Error Fix. We cover key concepts, practical examples, and best practices.

The Problem

JavaScript Proxy traps must follow strict invariants. Returning the wrong type from a trap, forgetting to forward to the target, or creating infinite recursion from trap-internal operations causes runtime errors or silent failures.

Quick Fix

Step 1: Return proper types from traps

const target = { name: 'Alice' };

// Wrong — get trap returns undefined for name
const proxy = new Proxy(target, {
    get(obj, prop) {
        if (prop === 'secret') return 'hidden';
        // Missing return for other properties
    }
});

console.log(proxy.name); // undefined

// Right — forward unknown properties
const proxy = new Proxy(target, {
    get(obj, prop) {
        if (prop === 'secret') return 'hidden';
        return Reflect.get(obj, prop);
    }
});

console.log(proxy.name); // Alice

Step 2: Avoid infinite recursion in traps

const target = { count: 0 };

// Wrong — trap accesses the proxy, not the target
const proxy = new Proxy(target, {
    get(obj, prop) {
        console.log(proxy.count); // infinite recursion!
        return Reflect.get(obj, prop);
    }
});

Avoid this — never access the proxy inside its own trap. Use the target parameter:

const proxy = new Proxy(target, {
    get(obj, prop) {
        console.log(obj[prop]); // access target directly
        return Reflect.get(obj, prop);
    }
});

console.log(proxy.count); // 0 (no recursion)

Step 3: Validate property values in set trap

const user = { age: 25 };

// Wrong — no validation
const proxy = new Proxy(user, {
    set(obj, prop, value) {
        obj[prop] = value;
        return true;
    }
});

proxy.age = -5;
console.log(proxy.age); // -5 (invalid)

// Right — validate before setting
const proxy = new Proxy(user, {
    set(obj, prop, value) {
        if (prop === 'age' && (typeof value !== 'number' || value < 0)) {
            throw new TypeError('Age must be a positive number');
        }
        obj[prop] = value;
        return true;
    }
});

proxy.age = -5; // TypeError: Age must be a positive number

Step 4: Use has trap for property hiding

const data = { public: 'visible', _secret: 'hidden' };

const proxy = new Proxy(data, {
    has(obj, prop) {
        if (prop.startsWith('_')) return false;
        return prop in obj;
    }
});

console.log('_secret' in proxy); // false
console.log('public' in proxy);  // true

Prevention

  • Always forward unknown property access using Reflect.get/Reflect.set
  • Never access the proxy inside its own trap — use the target object
  • Validate values in set traps before assigning
  • Return the correct boolean from set and deleteProperty traps
  • Use Reflect methods inside traps for correct default behavior

Common Mistakes with proxy trap

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks

These mistakes appear frequently in real-world JS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does my set trap throw TypeError: 'set' returned false?

The set trap must return true if the value was set, or false in strict mode to throw. If the return value is not a boolean when the target is non-extensible or the property is non-configurable, the engine throws TypeError.

Can a Proxy intercept array index access?

Yes. Array index access goes through the get trap. Use prop parameter to check if it is a numeric index: if (typeof prop === 'string' && /^\d+$/.test(prop)).

What happens if I don't implement a trap?

If a trap is missing, the proxy forwards the operation to the target automatically. You only need traps for the operations you want to intercept or customize.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro