Thanks for the reply. I knew about the single catch, but I was wondering for a more complex example what people would do. Say instead of hitting the same API server for each call, you are hitting different ones, each with their own error responses.
I guess you could have a single catch, and have a unique handler for each error type, but I found this was not as clean as I liked.
My solution to handle a scenario like this was storing an any errors caught mid promise chain in a variable and handling that error in a more procedural manner. I updated your example with how I would do it. Using async/await makes this way of handling errors cleaner than doing everything in the catch block imo.
constfetch=require("node-fetch");asyncfunctionmain(){try{consturl="https://jsonplaceholder.typicode.com";leterrconstpostResponse=awaitfetch(`${url}/posts/1`).catch(error=>err=error)if(err){/* Handle the posts error and return */}constpost=awaitpostResponse.json()constuserResponse=awaitfetch(`${url}/users/${post.userId}`).catch(error=>err=error);if(err){/* Handle the users error and return */}constuser=awaituserResponse.json();console.log("User who has posted the first post");console.log(user);}catch(error){console.error("Handle all other errors");console.error(error);}}main();
I understand what you are trying to do. You could use custom Error subclasses which allow you to keep handling errors in the catch part while still having some control over which kind of error is thrown instead of a generic one.
"use strict";classPostResponseErrorextendsError{constructor(...parameters){super(...parameters);this.name="PostResponseError";}}classUserResponseErrorextendsError{constructor(...parameters){super(...parameters);this.name="UserResponseError";}}asyncfunctionmain(){try{consturl="https://jsonplaceholder.typicode.com";constpostResponse=awaitfetch(`${url}/posts/1`);if(!postResponse.ok){thrownewPostResponseError("Error with the response");}constpost=awaitpostResponse.json();constuserResponse=awaitfetch(`${url}/users/${post.userId}`);if(!userResponse.ok){thrownewUserResponseError("Error with the response");}constuser=awaituserResponse.json();console.log("User who has posted the first post");console.log(user);}catch(error){if(errorinstanceofPostResponseError){console.log("Error with the post response");}elseif(errorinstanceofUserResponseError){console.log("Error with the user response");}else{console.error("Unexpected error");}console.error(error);}}main();
Thanks for the reply. I knew about the single catch, but I was wondering for a more complex example what people would do. Say instead of hitting the same API server for each call, you are hitting different ones, each with their own error responses.
I guess you could have a single catch, and have a unique handler for each error type, but I found this was not as clean as I liked.
My solution to handle a scenario like this was storing an any errors caught mid promise chain in a variable and handling that error in a more procedural manner. I updated your example with how I would do it. Using async/await makes this way of handling errors cleaner than doing everything in the catch block imo.
I understand what you are trying to do. You could use custom Error subclasses which allow you to keep handling errors in the catch part while still having some control over which kind of error is thrown instead of a generic one.
Beautiful 😍