Improve Error Handling in bookController.js
Created by: yosuasilalahi-jitera
Overview
This pull request improves the error handling logic in the bookController.js file. The changes ensure that all controller methods consistently handle errors and return appropriate HTTP status codes and messages.
Changes Explained
-
createBook: No changes required; already returns
400for errors. - getAllBooks: No changes required; already returns all books.
-
getBookById:
- Fixed misplaced
catchblock and improved error handling to return404if the book is not found, and400for other errors.
- Fixed misplaced
-
patchBook:
- Added logic to return
404if the book is not found, and400for validation errors. - Ensured the error handling is consistent with other methods.
- Added logic to return
-
updateBook: No changes required; already returns
400for errors. -
deleteBook: No changes required; already returns
404for errors.
These changes make the controller more robust and provide clearer feedback to API consumers.