Check the Status Before the Data

In the last lesson, you watched a 404 for XX arrive the same way as Portugal’s 200: the status line and headers first, and the body behind them. The status was already there at the first await. The planner’s code doesn’t look at it yet, though. It goes straight to the body.

That matters as soon as someone mistypes a country code, and with four friends typing codes, someone will. In this lesson, you’ll find out what a try block around fetch() really catches, write a getHolidays() function that checks the status before it trusts the data, and tell a server that answers “no” apart from a server that doesn’t answer at all. Start with the code you’d probably write first.

Catch a Mistyped Country Code

The planner will take country codes from the friends, so it has to cope when one is wrong. You already know how to handle an await that fails: put it in a try block and report the error in catch. Here’s holidays.mjs from Fetch One Country’s Holidays with a try block around the request, a loop that lists every holiday, and XX in place of PT:

Language: JavaScript Filename: holidays.mjs
const url =
  "https://date.nager.at/api/v4/Holidays/XX/2026";

try {
  const response = await fetch(url);
  const holidays = await response.json();
  for (const holiday of holidays) {
    console.log(holiday.date, holiday.name);
  }
} catch (error) {
  console.log("Could not load holidays:", error.message);
}

Save it, and before you run it, predict what it prints. Does the catch block run, and if it does, what does error.message say?

On November 27, 2026, the script printed one line:

Language: Shell
$ node holidays.mjs
Could not load holidays: holidays is not iterable

The catch block ran, so the script looks as if it handled the bad code. Read the message again, though. It says nothing about a 404, a country code, or Nager.Date.

fetch() didn’t reject. Its promise fulfilled with a Response, and response.json() parsed the body of the 404, the object {"title":"Unknown country code","status":404,...} you met in Sort Status Codes by Class.

Then the for...of loop threw a TypeError because a plain object isn’t iterable. The catch block caught an error from your loop. The server’s “no” never threw one.

This file holds the status and the body Nager.Date sent for XX on November 27, 2026. Save it as response-XX-404.json in your planner folder:

Language: JSON Filename: response-XX-404.json
{
  "status": 404,
  "body": {
    "title": "Unknown country code",
    "status": 404,
    "detail": "The country code provided is invalid or not recognized."
  }
}

This version of holidays.mjs builds the Response from the file instead of fetching it. It reads the file with readFile(), as in Fetch One Country’s Holidays. Then new Response() takes the body as text and the status in an options object and gives you the same kind of object fetch() would:

Language: JavaScript Filename: holidays.mjs
import { readFile } from "node:fs/promises";

const saved = JSON.parse(
  await readFile("response-XX-404.json", "utf8"),
);

try {
  const response = new Response(
    JSON.stringify(saved.body),
    { status: saved.status },
  );
  const holidays = await response.json();
  for (const holiday of holidays) {
    console.log(holiday.date, holiday.name);
  }
} catch (error) {
  console.log("Could not load holidays:", error.message);
}

Run it, and it prints the same line as the live request:

Language: Shell
$ node holidays.mjs
Could not load holidays: holidays is not iterable

For the next step, add the two logging lines right after the const response statement in this version.

You can confirm that the first await went through by logging the status and the .ok flag right after it, as you did for Portugal. Add the two lines from Fetch One Country’s Holidays:

Language: JavaScript Filename: holidays.mjs
const url =
  "https://date.nager.at/api/v4/Holidays/XX/2026";

try {
  const response = await fetch(url);
  console.log("status:", response.status);
  console.log("ok:", response.ok);
  const holidays = await response.json();
  for (const holiday of holidays) {
    console.log(holiday.date, holiday.name);
  }
} catch (error) {
  console.log("Could not load holidays:", error.message);
}

Run it again, and the two new lines print before the catch block’s message:

Language: Shell
$ node holidays.mjs
status: 404
ok: false
Could not load holidays: holidays is not iterable

The first await finished, and the script carried on with a Response whose status is 404 and whose .ok is false. A 404 is an answer. Nager.Date got the request, understood it, and replied. As far as fetch() is concerned, the request worked, so its promise fulfills.

fetch() rejects only when it has no usable Response to give you: when the request fails, for example because the network is down or the URL can’t be parsed, as you saw with a bare path in Node.js, or when the request is canceled.

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

Become a Member to join the conversation.