-
-
Notifications
You must be signed in to change notification settings - Fork 16
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Response in ended before the catch in RequestListener
#13
Comments
Hmm, I couldn't manage to replicate your issue, I've added tests describing your case, and they pass.
|
Ah, ok, I misread - the error is thrown properly, but you have problems writing the headers. Writing a |
If I want to write a different head, what are the options? For instance, I would like to send a 500 when something goes wrong in a resolver. |
You cannot write a different header. As mentioned before, it is already written and flushed at that point. You can only leave a log on your server and fix the |
Since re-writing the header is not possible, you can catch the error in 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 |
Thank you @enisdenjo! The example I have used is directly copied from the JSDoc for the |
🎉 This issue has been resolved in version 1.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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.The text was updated successfully, but these errors were encountered: