Registry / testing / express-rest-error

express-rest-error

JSON →
library1.2.1jsnpmunverified

Throws and handles HTTP errors (4xx) for Express REST APIs using service layer pattern. Version 1.2.1 is stable but has no releases since mid-2020. Provides simple factory functions (validationError, authRequired, etc.) and a middleware errorHandler. Differentiates from http-errors by being Express-specific and integrating with errorHandler middleware. Only 4 dependencies (http-status, lodash.iserror, ...). Not actively maintained but functional for many projects.

npm install express-rest-error
INSTALL
IMPORT
SIG · EXPRESS-REST-ERROR
E
express-rest-error
testingjavascriptv1.2.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

validationError
import { validationError } from 'express-rest-error'
const { validationError } = require('express-rest-error')
Package is ESM-only. CJS require will fail with ERR_REQUIRE_ESM.
authRequired
import { authRequired } from 'express-rest-error'
import authRequired from 'express-rest-error'
authRequired is a named export, not default.
errorHandler
import { errorHandler } from 'express-rest-error'
const errorHandler = require('express-rest-error').errorHandler
CJS require works only if the package had CJS build, but it's ESM-only.

Shows how to throw validationError and attach errorHandler middleware in an Express app. Includes async route.

import { validationError, errorHandler } from 'express-rest-error'; import express from 'express'; const app = express(); app.get('/hello/:name', async (req, res, next) => { try { if (!req.params.name) throw validationError('Name is required'); res.json({ message: `Hello ${req.params.name}!` }); } catch (err) { next(err); } }); app.use(errorHandler({ debug: process.env.NODE_ENV === 'development' })); app.listen(3000, () => console.log('Server ready'));
Debug
Known issues
breakingPackage uses ESM ("type": "module") since v1.2.1. CJS require() calls will throw ERR_REQUIRE_ESM.
fix
Use import syntax or upgrade to Node 14+ with type:'module'.
affects: >=1.2.1
deprecatedThe error handler's debug option includes stack traces in production by default, which could leak sensitive info.
fix
Set debug: false or conditionally enable only in development.
affects: all
gotchaPackage is not actively maintained; last update 2020. No support for ESM tree-shaking or TypeScript definitions.
fix
Consider alternatives like @curveball/http-errors or http-errors with express-http-error-handler.
affects: all
breakingIn v1.2.0, errorHandler signature changed from (err, req, res, next) to accepting options object.
fix
Update to app.use(errorHandler({ debug: true })) instead of app.use(errorHandler).
affects: >=1.2.0
gotchaCalling next(err) in async routes without try/catch leaves promise rejections unhandled.
fix
Wrap async route handlers in try/catch or use express-async-errors.
affects: all
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Trying to require('express-rest-error') from a CJS context.
fix
Use import { validationError } from 'express-rest-error' instead of require().
TypeError: errorHandler is not a function
Importing default instead of named export.
fix
Use import { errorHandler } from 'express-rest-error'.
Missing stack trace in error response
errorHandler called without options object.
fix
Call errorHandler({ debug: true }) to enable stack traces.
Upgrade
Version history
1.2.1latest on npm
Audit
Dependencies
http-statusrequiredUsed for setting status codes on error objects
lodash.iserrorrequiredUsed to check if a value is an Error instance
lodash.isplainobjectrequiredUsed to check if details argument is a plain object
Agent activity
4 hits · last 30 days
node
4
Resources
express-rest-error — npm install express-rest-error · libregistry