Files
main.tsmain
What you'll learn
- Write error-handling middleware with four parameters, mount it last, and answer every error in one shape
- Throw from synchronous and async handlers alike, relying on Express 5 to route both to the error handler
- Answer unmatched requests with a 404 middleware, and keep internal details out of 500 responses
Press Run tests to start the app. Its log appears here.Graded endpoints
Nothing wrong yet
The route threw an HttpError; the error handler turned it into the status and message it carries
express.json() failed before the route ran, and passed its error on: 400, as JSON rather than Express's HTML page
Thrown from an async handler: Express 5 catches it all the same
An error nobody planned for: 500, and nothing about the database reaches the client
201, as before
The failures stored nothing, and Milo got the next id
The catch-all after the routes answers in the API's own shape
A path that exists with a method no route has is a request nothing matched too