Skip to content

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 400 for errors.
  • getAllBooks: No changes required; already returns all books.
  • getBookById:
    • Fixed misplaced catch block and improved error handling to return 404 if the book is not found, and 400 for other errors.
  • patchBook:
    • Added logic to return 404 if the book is not found, and 400 for validation errors.
    • Ensured the error handling is consistent with other methods.
  • updateBook: No changes required; already returns 400 for errors.
  • deleteBook: No changes required; already returns 404 for errors.

These changes make the controller more robust and provide clearer feedback to API consumers.

Merge request reports