Last active
February 10, 2023 15:19
-
-
Save AlexXanderGrib/e8251adafab93e099403c4ce46a53e8f to your computer and use it in GitHub Desktop.
Error stack patching
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
import { wrapCall } from "./wrap.ts"; | |
const call = wrapCall('username=AlexXanderGrib', () => { | |
throw new Error(); | |
}) | |
call.async().catch(error => console.log(error.stack)); | |
// Error | |
// at example.ts:4:4 | |
// at username=AlexXanderGrib |
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
export function wrapCall<T, A extends readonly unknown[]>( | |
name: string, | |
fn: (...parameters: A) => T | |
) { | |
const sync = { | |
[name](...parameters: A): T { | |
return fn(...parameters); | |
} | |
}; | |
const async = { | |
async [name](...parameters: A): Promise<Awaited<T>> { | |
return await fn(...parameters); | |
} | |
}; | |
return { sync: sync[name], async: async[name] }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment