nestjs/terminus

MicroserviceHealthIndicator pingCheck() failed when error is undefined

osan15 opened this issue · 2 comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

In the pingCheck() function, if the error caught is of type "undefined / null", the pingCheck() function will return an unexpected error.

Minimum reproduction code

I reproduce the bug with an MQTT MicroService, in some cases it returns an "undefined" error if the service can't connect to the server.
To reproduce the case you have to catch an exception that is not an object.

Steps to reproduce

See below, I think the resolution of the problem is simple.

Expected behavior

The expected behavior is that the exception returned by the pingCheck() function should be an instance of HealthCheckError (MicroserviceHealthIndicator class).

I think the solution is to update the isError function to not throw an exception if the given error is undefined/null.

old:

export function isError(err: any): err is Error {
  return !!err.message;
}

new:

export function isError(err: any): err is Error {
  return !!err?.message;
}

In addition, the verification of the existence of the error is already done in the generateError() function

if (!error) {
      return;
    }

Package version

9.2.1

NestJS version

9.3.9

Node.js version

18.15.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Hi @osan15 , when you find some time can you create a minimal reproduction in a cloneable git repository, so we can better assess the problem?

Released with v10.1.0 🎉