Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Check headers have not already been sent in errorHandler #532

wants to merge 1 commit into from

3 participants


No description provided.

tj commented

what's the use-case for this one? if you just wanted to manipulate the response you should use the progressive header field api instead of writeHead


I had a case when an error was being thrown after headers had already been sent. Instead of handling the error as usual, the error handler was choking at this point and throwing it's own error. Adding a check to see if headers have already been sent seemed to fix it

tj commented

ah, well the res.end() there is invalid as well, node just happens to not throw but either way the response is likely complete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 10, 2012
  1. @nick
This page is out of date. Refresh to see the latest.
Showing with 3 additions and 1 deletion.
  1. +3 −1 lib/middleware/errorHandler.js
4 lib/middleware/errorHandler.js
@@ -70,7 +70,9 @@ exports = module.exports = function errorHandler(){
var error = { message: err.message, stack: err.stack };
for (var prop in err) error[prop] = err[prop];
var json = JSON.stringify({ error: error });
- res.setHeader('Content-Type', 'application/json');
+ if (!res.headerSent) {
+ res.setHeader('Content-Type', 'application/json');
+ }
// plain text
} else {
Something went wrong with that request. Please try again.