Errors, security, and observation

Handle async errors in Express 5

Use rejected promises and thrown errors correctly, then verify the Express 5 behavior you depend on.

9 minute lesson

~~~

Before choosing a tool or writing more code, make the requirement concrete. A database rejection reaches the centralized error path, while an expected missing note becomes a controlled not-found response.

Express 5 forwards rejected promises from async handlers to error middleware, reducing the need for repetitive wrappers. The useful target is not encyclopedic coverage. Make one deliberate choice, observe its consequences, and know which requirement would make you revise it.

A common failure is to catch every error only to log and forget it, leaving the request open. The happy path may still work, which makes the mistake easy to miss. throw unexpected failures, translate expected domain outcomes, and test promise rejection through the real router. Keep the first version small enough that every important input and output remains visible.

This supports the module goal: Make failures safe for users and useful for operators without leaking secrets or losing requests silently. Capture the request, command, trace, screenshot, test result, or other evidence that proves the result.

Make the repository reject and verify the request receives one safe response and the operator log retains the cause. Change one condition on purpose, predict the result, and compare the prediction with what actually happened.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →