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 and Signals 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
Add a ment  | 

1 Answer 1

Reset to default 11

Okay, 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