Skip to content

Instantly share code, notes, and snippets.

@igorjs
Created January 4, 2021 03:32
Show Gist options
  • Save igorjs/28719eaaa1309101afaa4e7455566441 to your computer and use it in GitHub Desktop.
Save igorjs/28719eaaa1309101afaa4e7455566441 to your computer and use it in GitHub Desktop.
export function safeThrow(
target: object,
key: string | symbol,
descriptor: TypedPropertyDescriptor<(req: Request, res: Response, next: NextFunction) => Promise<any>>) {
const fun = descriptor.value;
descriptor.value = async function () {
try {
await fun.apply(this, arguments);
} catch (err) {
arguments[2](err);
}
};
}
@safeThrow
private async get(req: Request, res: Response, next: NextFunction) {
throw { status: 404, message: 'Not supported' }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment