#GQL with exception filter - UnhandledPromiseRejectionWarning

5 messages · Page 1 of 1 (latest)

pulsar briar
#

we are using GQL and have a global exception filter , we would like to change the status code of the replies to 4xx instead of 200 with error message.
how can we change the status code ?
following code works , but throws this warning:

UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader

export class ExceptionsFilter
extends BaseExceptionFilter
implements GqlExceptionFilter
{

public catch(exception: unknown, host: ArgumentsHost): void {

const gqlHost = GqlArgumentsHost.create(host);
const response = gqlHost.getContext().res;
if (exception instanceof HttpException)
response.status(400).json({
statusCode: 400,
message: exception.getResponse(),
});

trim temple
#

we would like to change the status code of the replies to 4xx instead of 200 with error message.
wouldn't we all... That's part of the GQL Spec.

pulsar briar
#

but in our case one exception is actually thrown by a guard (ThrottlerGuard - for rate limit purposes) , and still it is handled as a GQL 200 response. Isn't there a way to return a 4xx in this case ?

ebon seal
#

nope, I have tried that =/ what you can do is return the status code in the body of the error, like I do in this case:

{
  "errors": [
    {
      "message": "Bad Request Exception",
      "code": "BAD_USER_INPUT",
      "exception": {
        "statusCode": 400,
        "message": [
          "CNPJ () is invalid!"
        ],
        "error": "Bad Request"
      }
    }
  ],
  "data": null
}
pulsar briar
#

why when an exception is thrown from a middelware , the correct status code is returned , but when thrown from a guard , it is handled as GQL reply ?