Complete the code to catch errors using try-catch.
try { const data = await fetch('https://api.example.com/data'); console.log(await data.json()); } catch ([1]) { console.error('Error fetching data:', error); }
The catch block uses error as the variable to hold the caught error.
Complete the code to log an info message using Deno's standard logger.
import { log } from "https://deno.land/std/log/mod.ts"; await log.setup({ handlers: { console: new log.handlers.ConsoleHandler("INFO"), }, loggers: { default: { level: "INFO", handlers: ["console"], }, }, }); log.[1]("This is an info message.");
The info method logs informational messages.
Fix the error in the async function to properly handle errors.
async function getData() {
try {
const response = await fetch('https://api.example.com/data');
if (!response.ok) throw new Error('Network response was not ok');
const data = await response.json();
return data;
} catch ([1]) {
console.log('Fetch failed:', error.message);
}
}The catch block must have parentheses around the error variable, like catch (error).
Fill both blanks to create a logger that writes error messages to a file.
import { log } from "https://deno.land/std/log/mod.ts"; await log.setup({ handlers: { file: new log.handlers.[1]("ERROR", { filename: "error.log", formatter: log.formatters.[2](), }), }, loggers: { default: { level: "ERROR", handlers: ["file"], }, }, });
Use FileHandler to write logs to a file and simple as a common formatter.
Fill all three blanks to create a custom error class and throw it with a message.
class [1] extends Error { constructor(message) { super(message); this.name = "[2]"; } } throw new [3]("Custom error occurred");
The class name and error name should match. The thrown error is an instance of the custom class.
