admin管理员组文章数量:1335176
I have a file, registerExceptionHandler.ts
, that does some logging in case of an unexpected crash of my application. The file looks like this:
const handleException = function(ex: Error): void {
console.log('Unexpected exception occured.', { ex });
process.exit(1);
};
export const registerExceptionHandler = function(): void {
process.on('uncaughtException', handleException);
process.on('unhandledRejection', handleException);
};
It works, but on the second process.on
I get the following error message:
Argument of type '"unhandledRejection"' is not assignable to parameter of type 'Signals'.
Why? What am I missing?
I have a file, registerExceptionHandler.ts
, that does some logging in case of an unexpected crash of my application. The file looks like this:
const handleException = function(ex: Error): void {
console.log('Unexpected exception occured.', { ex });
process.exit(1);
};
export const registerExceptionHandler = function(): void {
process.on('uncaughtException', handleException);
process.on('unhandledRejection', handleException);
};
It works, but on the second process.on
I get the following error message:
Argument of type '"unhandledRejection"' is not assignable to parameter of type 'Signals'.
Why? What am I missing?
Share Improve this question asked Jul 21, 2019 at 10:15 Golo RodenGolo Roden 151k102 gold badges314 silver badges442 bronze badges 2-
Which version of node and @types/node are you using? I just checked in a project I have and I have both -
unhandledRejection
andSignals
events types: i.sstatic/QZzi6.png – Mosh Feu Commented Jul 21, 2019 at 10:51 - 1 It's Node.js 10.15.3, TypeScript 3.5.3, and @types/node 12.6.8. – Golo Roden Commented Jul 21, 2019 at 10:56
1 Answer
Reset to default 11Okay, I got it: The signature of the handleException
function does not match what the unhandledRejection
case expects.
If I change my implementation to
const handleException = function(ex: Error): void {
console.log('Unexpected exception occured.', { reason: ex.message, ex });
process.exit(1);
};
const handleRejectedPromise = function(
reason: any,
promise: Promise<any>
): void {
console.log('Unexpected exception occured.', { reason, ex: promise });
process.exit(1);
};
export const registerExceptionHandler = function(): void {
process.on('uncaughtException', handleException);
process.on('unhandledRejection', handleRejectedPromise);
};
things work as expected.
本文标签: javascriptunhandledRejection is not assignable to parameter of type SignalsStack Overflow
版权声明:本文标题:javascript - unhandledRejection is not assignable to parameter of type Signals - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742244513a2439043.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论