JavaScript Proxy Handler Trap Error Fix
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
settraps before assigning - Return the correct boolean from
setanddeletePropertytraps - Use
Reflectmethods inside traps for correct default behavior
Common Mistakes with proxy trap
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro