I have a method in Node JS which reads a file containing JSON data and finds a product with specific ID.
async getProductwithId(id) {
try {
let rawData = fs.readFileSync("data/products.json");
let data = JSON.parse(rawData);
for (const element of data) {
if (id === element.productId) {
return element;
}
}
throw new ProductDoesNotExistError("No Such Product Exists");
} catch (error) {
throw new FileReadingError("Error Reading File");
}
}
where ProductDoesNotExistError and FileReadingError both extend Error. I have put try/catch for the fs.readFileSync()
the problem is even if i have ProductDoesNotExistError, it's sending FileReadingError. I want to handle here the FileReadingError only and not ProductDoesNotExistError. I will let the callling function handle the ProductDoesNotExistError. How do I achieve this functionality.