Skip to content

Truncated error response when using reply.code(undefined) #2078

@feugy

Description

@feugy

🐛 Bug Report

When setting the response status code to undefined inside error handler, response returned is truncated.

This bug was unveiled while working on an issue with fastifh-gql default error handler, which set the response code to err.statusCode, that could be undefined.

To Reproduce

Codesandbox

Paste your code here:

const fastify = require("fastify");
const axios = require("axios");

async function start() {
  const app = fastify({ logger: true });

  app.setErrorHandler(async (err, request, reply) => {
    reply.code(undefined);
    return { errors: [err.message] };
  });

  app.get("/", async (request, reply) => {
    throw new Error("This message will be truncated");
  });

  const port = 8080;
  try {
    await app.listen(port);
  } catch (err) {
    app.log.error(err);
    process.exit(1);
  }

  await axios(`http://localhost:${port}`);
}

if (require.main === module) {
  start();
}

You'll get in return:

(node:1727) UnhandledPromiseRejectionWarning: Error: Parse Error
    at Socket.socketOnData (_http_client.js:442:20)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:287:12)
    at readableAddChunk (_stream_readable.js:268:11)
    at Socket.Readable.push (_stream_readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:94:17)

On a browser such as Firefox or Chrome, the response is truncated.

Expected behavior

The response should not be truncated.

Your Environment

  • node version: 13.3
  • fastify version: >=2.11.0
  • os: Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions