Response in ended before the catch in `RequestListener`
See original GitHub issueIn 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:
- Created 2 years ago
- Comments:7 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thank you @enisdenjo! The example I have used is directly copied from the JSDoc for the
Handler
. I have submitted a PR for this.Since re-writing the header is not possible, you can catch the error in
onNext
and populate theerrors
GraphQL result field to transmit to the client through thenext
message.Something around the edges:
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 thenext
message, are not terminal - the client will not end and the operation will proceed blindly.