Expected an error object to be thrown
68 await product.save()
69 return cart
70 } catch (error) {
71 throw { 72 message: `Error adding item to cart: ${error.message}`, 73 statusCode: error.statusCode, 74 } 75 }
76}
77
Expected an error object to be thrown
40 }
41 }
42 if (product.inventoryCount < quantity) {
43 throw { 44 message: 45 'Not enough inventory for the product, please reduce quantity or choose a different product.', 46 statusCode: 409, 47 } 48 }
49
50 let cart = await Cart.findOne({ user: userId })
Expected an error object to be thrown
34 // const product = await Product.findOne({ _id: productId })
35 const product = await Product.findById(productId)
36 if (!product) {
37 throw { 38 message: 'Product not found', 39 statusCode: 404, 40 } 41 }
42 if (product.inventoryCount < quantity) {
43 throw {
Description
It is considered good practice to only throw
the Error
object itself or an object using the Error
object as base objects for user-defined exceptions.
The benefit of Error
objects is that they automatically keep track of where they were built and originated.
This rule restricts what can be thrown as an exception.
Bad Practice
throw "error";
throw 0;
throw undefined;
throw null;
var err = new Error();
throw "an " + err;
// err is recast to a string literal
var err = new Error();
throw `${err}`
Recommended
throw new Error();
throw new Error("error");
var e = new Error("error");
throw e;
try {
throw new Error("error");
} catch (e) {
throw e;
}