Skip to content

GraphQL doesn't work well with BaseExceptionFilter #5958

@nirga

Description

@nirga

Bug Report

Current behavior

When throwing an exception within a GraphQL resolver, and then using a global Exception filter that extends BaseExceptionFilter , there's an exception inside BaseExceptionFilter.

{
  "errors": [
    {
      "message": "response.status is not a function",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "getUser"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: response.status is not a function",
            "    at ExpressAdapter.reply (/Users/nirga/tutorials/nest-test/node_modules/@nestjs/platform-express/adapters/express-adapter.js:19:22)",
            "    at CustomExceptionFilter.catch (/Users/nirga/tutorials/nest-test/node_modules/@nestjs/core/exceptions/base-exception-filter.js:26:24)",
            "    at CustomExceptionFilter.catch (/Users/nirga/tutorials/nest-test/dist/src/graphql-exception.filter.js:19:20)",
            "    at ExternalExceptionsHandler.invokeCustomFilters (/Users/nirga/tutorials/nest-test/node_modules/@nestjs/core/exceptions/external-exceptions-handler.js:34:32)",
            "    at ExternalExceptionsHandler.next (/Users/nirga/tutorials/nest-test/node_modules/@nestjs/core/exceptions/external-exceptions-handler.js:13:29)",
            "    at /Users/nirga/tutorials/nest-test/node_modules/@nestjs/core/helpers/external-proxy.js:14:42",
            "    at processTicksAndRejections (internal/process/task_queues.js:93:5)"
          ]
        }
      }
    }
  ],
  "data": {
    "getUser": null
  }
}

Input Code

CustomExceptionFilter

@Catch(HttpException)
export class CustomExceptionFilter extends BaseExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    super.catch(exception, host);
  }
}

And then throw an error inside some GraphQL resolver:

@Resolver('User')
export class UserResolver {
  @Query()
  async getUser() {
    throw new UnauthorizedException();
  }
}

Expected behavior

The original error should have been the one returned and not the exception from within BaseExceptionFilter.

Possible Solution

When BaseExceptionFilter handles unknown exceptions, it uses host.getArgByIndex(1) to build the response. However, this may be null for GraphQL queries as their execution context is different.

Environment


Nest version: 7.5.1

 
For Tooling issues:
- Node version: 14  
- Platform: Mac 

Others:

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue has not been looked into

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions