question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Response in ended before the catch in `RequestListener`

See original GitHub issue

In the following example the server is not returning 500 Internal Server Error. Instead the server throws an error (Error [ERR_HTTP_HEADERS_SENT]: Cannot render headers after they are sent to the client) and the process is killed.

import http from 'http';
import { createHandler } from 'graphql-sse';

const handler = createHandler({
  ...
  onNext: () => {
    throw new Error('Not implemented');
  },
});

http.createServer(async (req, res) => {
  try {
    await handler(req, res);
  } catch (err) {
    res.writeHead(500, 'Internal Server Error').end();
  }
});

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
asadhazaracommented, Jan 1, 2022

Thank you @enisdenjo! The example I have used is directly copied from the JSDoc for the Handler. I have submitted a PR for this.

1reaction
enisdenjocommented, Dec 31, 2021

Since re-writing the header is not possible, you can catch the error in onNext and populate the errors GraphQL result field to transmit to the client through the next message.

Something around the edges:

import http from 'http';
import { createHandler } from 'graphql-sse';

const handler = createHandler({
  // ... your other options ...
  onNext: () => {
    try {
      mightThrowAnError();
    } catch (err) {
      return {
        errors: [new GraphQLError(err)],
      };
    }
  },
});

http.createServer(async (req, res) => {
  try {
    await handler(req, res);
  } catch (err) {
    if (!res.headersSent) {
      res.writeHead(500, 'Internal Server Error').end();
    } else {
      console.error('Internal Server Error', err);
    }
  }
});

But beware, I do NOT recommend this. You should absolutely make sure that nothing is thrown from onNext. Execution errors, errors that are a part of the next message, are not terminal - the client will not end and the operation will proceed blindly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NodeJS http: Wait for another response in a request listener ...
I thought I must finish the response before the request listener callback returns; it seems I do not. I moved the res.end() to...
Read more >
Returning a promise from requestListener so we know when ...
Here's the issue I think it might cause: If a Promise is returned and the caller does nothing to handle it (no await...
Read more >
Anatomy of an HTTP Transaction | Node.js
When an HTTP request hits the server, node calls the request handler function with a few handy objects for dealing with the transaction,...
Read more >
org.eclipse.jetty.client.api.Request.listener java code examples
Response > fut = SettableFuture.create(); final TimingListener listener = new TimingListener(fut, options.maxResponseBytes); toJettyRequest(request) .
Read more >
Create an HTTP Server with Node.js : Beginner's Guide
As mentioned before, a web server accepts requests from different ... The response object is responsible for returning HTTP responses from ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found