Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
feugy opened this issue Feb 3, 2020 · 2 comments 路 Fixed by #2082
Closed

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

feugy opened this issue Feb 3, 2020 · 2 comments 路 Fixed by #2082
Labels
good first issue Good for newcomers

Comments

@feugy
Copy link

feugy commented Feb 3, 2020

馃悰 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
@mcollina
Copy link
Member

mcollina commented Feb 3, 2020

I think the problem is that reply.code(undefined) should not be allowed.
I think we should make it throw if a non-valid status code is passed in.

Would that work?

@mcollina mcollina added the good first issue Good for newcomers label Feb 3, 2020
@mcollina
Copy link
Member

mcollina commented Feb 3, 2020

@feugy can you also open an issue on fastify-gql? It should not pass an undefined status code.

feugy added a commit to feugy/fastify-gql that referenced this issue Feb 4, 2020
Only set error response status code when it is defined. Otherwise response will be truncated. See fastify/fastify#2078
mcollina pushed a commit to mercurius-js/mercurius that referenced this issue Feb 4, 2020
* fix: error handler setting undefined status code

Only set error response status code when it is defined. Otherwise response will be truncated. See fastify/fastify#2078

* chore: add tests
leorossi added a commit to leorossi/fastify that referenced this issue Feb 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants