Skip to content
Advertisement

NestJS and TypeORM Exception Filter: Get Status is not a function

I’m developing an app using NestJS and TypeORM. My goal is to catch TypeORM errors and it could be done using exception filters.

But my problem is, I’m encountering this error:

(node:345) UnhandledPromiseRejectionWarning: TypeError: exception.getStatus is not a function

This is similar to this github post discussing the problem but it’s not working on my end.

Here’s my setup:

JavaScript

I’ve declared this globally so,

main.ts

app.useGlobalFilters(new TypeORMQueryExceptionFilter())

app.module.ts

JavaScript

If you have an idea resolving why getStatus() of HttpException is undefined, it would be a big help.

Update

As this line suggests catch(exception: HttpException, host: ArgumentsHost) { in relation to @Catch(QueryFailedError), I could assume that HttpException is the wrong type for param exception. It should be,if ever, catch(exception: QueryFailedError, host: ArgumentsHost) { instead.

Thus, since the QueryFailedError data structure is this:

JavaScript

I believe that const status = exception.getStatus(); is not needed so we could re-write or eliminate const status = exception.getStatus() to const status = exception.driverError(). This is relation to the example here.

Since QueryFailedError doesn’t represent any http error codes in its properties, one solution could have to hard code the http status response like this:

JavaScript

Can someone validate this as it could be I just did something wrong as exception: HttpException should be working anyway?

Advertisement

Answer

instances of QueryFailedError doesn’t has the method getStatus. Fix the types of your filter:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement