throw(...args) { throw createError(...args); }, onerror(err) { // don't do anything if there is no error. // this allows you to pass `this.onerror` // to node-style callbacks.s allows you to pass `this.onerror` // to node-style callbacks. if (null == err) return;
if (!(err instanceofError)) err = newError(util.format('non-error thrown: %j', err));
let headerSent = false; if (this.headerSent || !this.writable) { headerSent = err.headerSent = true; }
// delegate this.app.emit('error', err, this);
// nothing we can do here other // than delegate to the app-level // handler and log. if (headerSent) { return; }
const { res } = this;
// first unset all headers if (typeof res.getHeaderNames === 'function') { res.getHeaderNames().forEach(name => res.removeHeader(name)); } else { res._headers = {}; // Node < 7.7 }
// then set those specified this.set(err.headers);
// force text/plain this.type = 'text';
// ENOENT support if ('ENOENT' == err.code) err.status = 404;
// default to 500 if ('number' != typeof err.status || !statuses[err.status]) err.status = 500;