Facebook Pixel

What is a custom ApiError class?

A subclass of Error with a status and message. Use it to throw new ApiError(404, 'User not found'). The error handler reads err.status and err.message, so errors are consistent and typed.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Express Error-Handling Middleware Explained

It has four parameters (err, req, res, next). Express detects this arity and uses it for errors. Register it last. In handlers, throw or call next(err). Express then jumps to the error handler.

Use an asyncHandler wrapper: Promise.resolve(fn(req, res, next)).catch(next). This catches promise rejections and passes them to the error handler. Express 5 catches async errors automatically.

Stack traces expose internal file paths, library versions, and logic. Attackers can use them to find weaknesses. In production, send a safe message; log the full error internally. Use an expose flag on ApiError for safe-to-show errors.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.