Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Follow an Error to the Handler

In this lesson, you followed errors through a copy of the wall’s API to see which ones Express catches, where it takes them, and which ones it never sees.

Key Points

  • Express catches an error thrown while it calls your function. For problem 42, store.get() threw a NotFoundError before the GET /problems/:id handler’s own check could run, and curl got Express’s HTML page with a 500.
  • An error skips every ordinary function. Express stops the request’s walk and carries the error down the rest of the list. Ordinary middleware and handlers take at most three parameters, and the error passes all of them, even a function that would run for every path.
  • It stops at the first function with four parameters, the error first. With none in the app, the error falls to the bottom of the list, where Express’s default handler sends its HTML page. With the toy traceError() registered below the routers, problem 42 got the table’s 404 with problem_not_found, and the server’s terminal showed the line traceError() logged.
  • Express 5 forwards a rejected promise from an async handler. The POST /problems handler has no catch block either. A second Chalk Dust made store.create() reject with a ConflictError. The handler awaited it, so its own promise rejected, and Express sent the error down the same path, so curl got 409 with problem_exists.
  • Older code wraps every async handler for Express 4. You’ll often see try and catch, or a helper, around every async handler, only to pass errors on. Express 4 needed that, and Express 5 doesn’t.
  • A function can put an error on the path itself by calling next() with the error.
  • A throw in a timer callback escapes. Express takes an error in three ways: a throw while it calls your function, a rejection of the promise your function returns, even a late one, or next() with the error. A throw new Error("Thrown later") inside a setTimeout() callback is none of those. The duplicate still got its 409 from traceError(). Then nothing caught the timer’s error, so Node stopped the server and the next request couldn’t connect.
  • A rejected promise that’s neither awaited nor returned escapes too. Without await on store.create(), TypeScript’s check complained, but Node ran the file anyway. The handler sent the pending promise, which has no fields to turn into JSON, so curl got 201 with an empty object, {}, and the client was told the create worked. It didn’t. The promise had rejected with the ConflictError, and nothing was saved. Nobody was listening, so the server stopped again.

A throw while Express calls your function, a rejection of the promise it returns, and next() with an error all skip the ordinary functions and reach the four-parameter function after the routes. A throw in a timer, or a rejected promise nobody awaits or returns, reaches nothing, and the server stops. So await or return every promise.

The Final Code

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In the last lesson, the store learned to throw errors of its own, and toErrorResponse() learned to turn any error into a row of the table. Nothing calls it yet, though.

00:09 In this video, you’ll follow a thrown error through the wall’s API, to find the one place that could catch errors from every route.

00:18 Ask for problem 42. The store throws a NotFoundError before the handler’s own check can run, and curl gets Express’s HTML page with a 500. So Express caught the error.

00:32 Where did it go in the chain?

00:35 When a function throws, Express stops the walk you followed before. It catches the error and carries it down the rest of the list. Ordinary middleware and handlers take at most three parameters, and the error skips every one of them.

00:50 These routes don’t match anyway, but even a function for every path would be skipped. It stops only at a function with four parameters, the error first. The wall’s app has none, so the error falls to the bottom, where Express’s default handler sends its HTML page.

01:08 In a copy of the project, add one below the routers. traceError() takes the error first, logs a line, and answers with whatever toErrorResponse() returns for it.

01:19 It’s a toy for this video, so you don’t need to type it. Ask for problem 42 again. This time, traceError() catches it. Curl gets a 404 in the API’s shape, and the server’s terminal shows the line it logged.

01:35 The handler that creates problems is an async function, with no catch block either. Post a second Chalk Dust. Creating it in the store rejects with a ConflictError, because the name is taken.

01:47 The handler awaits it, so the handler’s own promise rejects. Express 5 watches the promise your handler returns. It sends the rejection down the same path, and curl gets the 409.

01:59 In older code, you’ll often see every async handler wrapped in try/catch, or in a helper, only to pass errors on. Express 4 needed that, and Express 5 doesn’t.

02:11 A function can also put an error on this path itself, by calling next() with the error.

02:18 Now, inside a setTimeout() callback, throw an error that says Thrown later, and post the same duplicate. Your turn: pause the video and predict where the timer’s error ends up.

02:35 The duplicate still gets its 409 from the toy handler. A moment later, the timer’s callback runs and throws. Express takes an error in three ways: a throw while it calls your function, a rejection of the promise your function returns, even later, or next() with the error.

02:54 Node runs the timer after your function has returned, so this throw is none of those, and nothing catches it. Node stops the server, and the next request can’t connect.

03:05 A rejected promise that’s neither awaited nor returned escapes too. Take the timer out, and drop the await from the line that calls the store. TypeScript’s check complains, but Node runs the file anyway.

03:18 The handler answers 201 with an empty object. It sent the pending promise, which has no fields to turn into JSON, so the client hears the create worked. It didn’t.

03:29 That promise has rejected with the ConflictError, and nothing was saved. Nobody is listening, so the server stops again.

03:39 An error in your API takes one of two paths. A throw while Express calls your function, a rejection of the promise your function returns, and next() with an error all skip the ordinary functions and reach the four-parameter function after the routes.

03:55 A throw in a timer, or a rejected promise nobody awaits or returns, reaches nothing, and the server stops. So await or return every promise.

04:06 That one function after the routes is where every error can be answered. In the next lesson, Write One Error Handler, you’ll replace the toy with the real one for the wall’s API.

Become a Member to join the conversation.