Created
November 8, 2022 08:45
-
-
Save colelawrence/6b266eded700b4142410cba619c17071 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Generate a fragile object that will throw error at any operation. | |
*/ | |
export function createErrorObj<T = any>(error: string): T { | |
return new Proxy( | |
{}, | |
{ | |
get(target, prop, receiver: unknown) { | |
throw new Error( | |
error + | |
`\nYou are trying to access a property "obj.${prop.toString()}".` | |
); | |
}, | |
set(target, prop, val: unknown, receiver: unknown) { | |
throw new Error( | |
error + | |
`\nYou are trying to set a property "obj.${prop.toString()}" equal to ${val}.` | |
); | |
}, | |
apply(target, thisArg: unknown, argumentsList: unknown[]) { | |
throw new Error( | |
error + `\nYou are trying to call me with ${argumentsList}.` | |
); | |
}, | |
} | |
) as T; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment