Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🥅 Imrpove Error Catch #604

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions backend/controllers/appControllers/clientController/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ const remove = async (req, res) => {
result,
message: 'Successfully Deleted the client by id: ' + id,
});
} catch (err) {
} catch (error) {
return res.status(500).json({
success: false,
result: null,
message: 'Oops there is an Error',
error: err,
message: error.message,
error: error,
});
}
};
Expand Down
14 changes: 7 additions & 7 deletions backend/controllers/appControllers/invoiceController/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,23 @@ const create = async (req, res) => {
result: updateResult,
message: 'Invoice created successfully',
});
} catch (err) {
console.log(err);
// If err is thrown by Mongoose due to required validations
if (err.name == 'ValidationError') {
} catch (error) {
console.log(error);
// If error is thrown by Mongoose due to required validations
if (error.name == 'ValidationError') {
return res.status(400).json({
success: false,
result: null,
error: err,
error: error,
message: 'Required fields are not supplied',
});
} else {
// Server Error
return res.status(500).json({
success: false,
result: null,
error: err,
message: 'Oops there is an Error',
error: error,
message: error.message,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,38 +52,38 @@ const sendMail = async (req, res) => {
}
}
)
.catch((err) => {
.catch((error) => {
return res.status(500).json({
success: false,
result: null,
error: err,
message: 'Oops there is an Error',
error: error,
message: error.message,
});
});
} catch (err) {
// If err is thrown by Mongoose due to required validations
if (err.name == 'ValidationError') {
} catch (error) {
// If error is thrown by Mongoose due to required validations
if (error.name == 'ValidationError') {
return res.status(400).json({
success: false,
result: null,
error: err,
error: error,
message: 'Required fields are not supplied',
});
} else if (err.name == 'BSONTypeError') {
// If err is thrown by Mongoose due to invalid ID
} else if (error.name == 'BSONTypeError') {
// If error is thrown by Mongoose due to invalid ID
return res.status(400).json({
success: false,
result: null,
error: err,
error: error,
message: 'Invalid ID',
});
} else {
// Server Error
return res.status(500).json({
success: false,
result: null,
error: err,
message: 'Oops there is an Error',
error: error,
message: error.message,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ const remove = async (req, res) => {
result: deletedInvoice,
message: 'Invoice deleted successfully',
});
} catch (err) {
} catch (error) {
return res.status(500).json({
success: false,
result: null,
error: err,
message: 'Oops there is an Error',
error: error,
message: error.message,
});
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ const summary = async (req, res) => {
return res.status(500).json({
success: false,
result: null,
message: 'Oops there is an Error',
message: error.message,
error: error,
});
}
Expand Down
12 changes: 6 additions & 6 deletions backend/controllers/appControllers/invoiceController/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,22 @@ const update = async (req, res) => {
result,
message: 'we update this document by this id: ' + req.params.id,
});
} catch (err) {
// If err is thrown by Mongoose due to required validations
if (err.name == 'ValidationError') {
} catch (error) {
// If error is thrown by Mongoose due to required validations
if (error.name == 'ValidationError') {
return res.status(400).json({
success: false,
result: null,
error: err,
error: error,
message: 'Required fields are not supplied',
});
} else {
// Server Error
return res.status(500).json({
success: false,
result: null,
error: err,
message: 'Oops there is an Error',
error: error,
message: error.message,
});
}
}
Expand Down
8 changes: 4 additions & 4 deletions backend/controllers/appControllers/offerController/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ const create = async (req, res) => {
result: updateResult,
message: 'Offer created successfully',
});
} catch (err) {
// If err is thrown by Mongoose due to required validations
if (err.name == 'ValidationError') {
} catch (error) {
// If error is thrown by Mongoose due to required validations
if (error.name == 'ValidationError') {
return res.status(400).json({
success: false,
result: null,
Expand All @@ -70,7 +70,7 @@ const create = async (req, res) => {
return res.status(500).json({
success: false,
result: null,
message: err.message,
message: error.message,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const summary = async (req, res) => {
return res.status(500).json({
success: false,
result: null,
message: 'Oops there is an Error',
message: error.message,
error: error,
});
}
Expand Down
10 changes: 5 additions & 5 deletions backend/controllers/appControllers/offerController/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ const update = async (req, res) => {
result,
message: 'we update this document by this id: ' + req.params.id,
});
} catch (err) {
// If err is thrown by Mongoose due to required validations
console.log(err);
if (err.name == 'ValidationError') {
} catch (error) {
// If error is thrown by Mongoose due to required validations
console.log(error);
if (error.name == 'ValidationError') {
return res.status(400).json({
success: false,
result: null,
Expand All @@ -69,7 +69,7 @@ const update = async (req, res) => {
return res.status(500).json({
success: false,
result: null,
message: 'Oops there is an Error',
message: error.message,
});
}
}
Expand Down
12 changes: 6 additions & 6 deletions backend/controllers/appControllers/paymentController/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,22 @@ const create = async (req, res) => {
result: updatePath,
message: 'Payment Invoice created successfully',
});
} catch (err) {
// If err is thrown by Mongoose due to required validations
if (err.name == 'ValidationError') {
} catch (error) {
// If error is thrown by Mongoose due to required validations
if (error.name == 'ValidationError') {
res.status(400).json({
success: false,
result: null,
message: 'Required fields are not supplied',
error: err,
error: error,
});
} else {
// Server Error
res.status(500).json({
success: false,
result: null,
message: 'Oops there is an Error',
error: err,
message: error.message,
error: error,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ const remove = async (req, res) => {
result,
message: 'Successfully Deleted the document by id: ' + req.params.id,
});
} catch (err) {
} catch (error) {
return res.status(500).json({
success: false,
result: null,
message: 'Oops there is an Error',
error: err,
message: error.message,
error: error,
});
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const summary = async (req, res) => {
return res.status(500).json({
success: false,
result: null,
message: 'Oops there is an Error',
message: error.message,
error: error,
});
}
Expand Down
12 changes: 6 additions & 6 deletions backend/controllers/appControllers/paymentController/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,22 @@ const update = async (req, res) => {
result,
message: 'Successfully updated the Payment ',
});
} catch (err) {
// If err is thrown by Mongoose due to required validations
if (err.name == 'ValidationError') {
} catch (error) {
// If error is thrown by Mongoose due to required validations
if (error.name == 'ValidationError') {
res.status(400).json({
success: false,
result: null,
message: 'Required fields are not supplied',
error: err,
error: error,
});
} else {
// Server Error
res.status(500).json({
success: false,
result: null,
message: err.message,
error: err,
message: error.message,
error: error,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ const convertQuoteToInvoice = async (req, res) => {
note: quote.note,
};

invoiceData['createdBy'] = req.admin._id;
// Creating a new document in the collection

// Create the invoice document
const invoice = await new InvoiceModel(invoiceData).save();

// Mark the quote as converted
quote['createdBy'] = req.admin._id;
quote.converted = true;
await quote.save();

Expand All @@ -63,19 +67,21 @@ const convertQuoteToInvoice = async (req, res) => {
result: quote,
message: 'Successfully converted quote to invoice',
});
} catch (err) {
} catch (error) {
// If error is because of Invalid ObjectId
if (err.kind == 'ObjectId') {
if (error.kind == 'ObjectId') {
return res.status(400).json({
success: false,
result: null,
message: 'Invalid ID format',
error: error,
});
} else {
return res.status(500).json({
success: false,
result: null,
message: 'Oops there is an Errorr',
error,
message: error.message,
});
}
}
Expand Down
8 changes: 4 additions & 4 deletions backend/controllers/appControllers/quoteController/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ const create = async (req, res) => {
result: updateResult,
message: 'Quote created successfully',
});
} catch (err) {
// If err is thrown by Mongoose due to required validations
if (err.name == 'ValidationError') {
} catch (error) {
// If error is thrown by Mongoose due to required validations
if (error.name == 'ValidationError') {
return res.status(400).json({
success: false,
result: null,
Expand All @@ -69,7 +69,7 @@ const create = async (req, res) => {
return res.status(500).json({
success: false,
result: null,
message: 'Oops there is an Error',
message: error.message,
});
}
}
Expand Down
Loading