Improve Error Handling in bookController.js
Created by: chi-jitera
Overview
This pull request improves the error handling logic in the /controllers/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; method does not have error handling, which is acceptable if
bookService.getAllBooks()never throws. -
getBookById:
- Fixed misplaced
catchblock and closing braces. - Ensured that errors are caught and a
404status is returned if the book is not found, otherwise a400for other errors.
- Fixed misplaced
-
patchBook:
- Added logic to return
404if the error message is'Book not found', otherwise returns400for validation errors. - Ensured consistent error handling and response structure.
- Added logic to return
-
updateBook:
- No changes required; already returns
400for errors.
- No changes required; already returns
-
deleteBook:
- No changes required; already returns
404for errors.
- No changes required; already returns
Summary
These changes make the error handling in bookController.js more robust and consistent, improving API reliability and client experience.