Development-only error handler middleware for Express.js applications with comprehensive error formatting and content negotiation.
npx @tessl/cli install tessl/npm-errorhandler@1.5.0Error Handler is a development-only Express.js/Connect middleware that provides comprehensive error handling with content negotiation support. It displays detailed error information with full stack traces and object details, making it an essential debugging tool for Express.js applications during development.
npm install errorhandlerconst errorHandler = require("errorhandler");For ES modules:
import errorHandler from "errorhandler";const express = require("express");
const errorHandler = require("errorhandler");
const app = express();
// Only use errorhandler in development
if (process.env.NODE_ENV === "development") {
app.use(errorHandler());
}
// Example route that might throw an error
app.get("/error", (req, res, next) => {
const err = new Error("Something went wrong!");
err.status = 500;
next(err);
});
app.listen(3000);Creates Express/Connect error handling middleware that provides comprehensive error reporting with content negotiation.
/**
* Creates error handling middleware for Express/Connect applications
* @param {Object} [options] - Configuration options
* @param {boolean|Function} [options.log] - Logging configuration
* @returns {Function} Express/Connect error middleware with signature (err, req, res, next) => void
*/
function errorHandler(options);Parameters:
options (Object, optional): Configuration options
options.log (Boolean|Function, optional): Controls error logging behavior
true (default in non-test environments): Log errors to console.errorfalse: Disable error loggingFunction: Custom log handler with signature (err, str, req, res) => voidReturns: Function - Express/Connect middleware that handles errors
Usage Examples:
// Basic usage with default logging
app.use(errorHandler());
// Disable logging
app.use(errorHandler({ log: false }));
// Custom logging function
app.use(errorHandler({
log: (err, str, req, res) => {
console.log(`Custom log: ${str}`);
}
}));Customizable template title for HTML error pages.
/**
* Template title for HTML error pages
* @type {string}
* @default "Connect"
*/
errorHandler.title;Usage Example:
const errorHandler = require("errorhandler");
// Customize the title shown in HTML error pages
errorHandler.title = "My App Development";
app.use(errorHandler());The middleware automatically detects the client's preferred content type via the Accept header and responds accordingly:
text/html)Returns a formatted HTML page with:
application/json)Returns a JSON object containing:
error.message: The error messageerror.stack: The full stack traceExample JSON Response:
{
"error": {
"message": "Something went wrong!",
"stack": "Error: Something went wrong!\n at /app/routes.js:10:15\n at ...",
"status": 500
}
}Returns the error as plain text:
stack propertyutil.inspect() outputtoString(): Uses the custom methodThe middleware respects error status codes in the following priority:
err.status (preferred)err.statusCode (fallback)500 (default for errors)The middleware handles different error types:
util.inspect() for detailed outputtoString() methodThe middleware includes built-in security measures:
X-Content-Type-Options: nosniff header to prevent MIME sniffing attacksThe middleware automatically adapts to different environments:
Important: This middleware should never be used in production environments as it exposes sensitive application details including stack traces and internal application structure.
/**
* Configuration options for the error handler
*/
interface ErrorHandlerOptions {
/** Controls error logging behavior */
log?: boolean | ((err: Error, str: string, req: Request, res: Response) => void);
}
/**
* Express/Connect error handling middleware signature
*/
interface ErrorMiddleware {
(err: Error, req: Request, res: Response, next: NextFunction): void;
}