Skip to content

Instantly share code, notes, and snippets.

@colelawrence
Created November 8, 2022 08:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colelawrence/6b266eded700b4142410cba619c17071 to your computer and use it in GitHub Desktop.
Save colelawrence/6b266eded700b4142410cba619c17071 to your computer and use it in GitHub Desktop.
/**
* 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