Skip to content

Conversation

@vamshi1188
Copy link

This PR improves the GetBookByID method in the BookModel by adding:

✅ Input validation to ensure bookID is not empty

✅ Proper error handling for SQL errors

✅ Graceful handling of sql.ErrNoRows when no matching book is found

✅ Contextual error messages for easier debugging

These changes make the function safer, more robust, and production-ready.

@vamshi1188 vamshi1188 requested a review from khareyash05 as a code owner June 17, 2025 20:03
@keploy
Copy link

keploy bot commented Jun 17, 2025

Keploy failed to create test cases for this PR. For retrying, please click here

@vamshi1188
Copy link
Author

@khareyash05 i run the program and there is no errors , once check it and please merge it , this is my fir apifellowship pr

@SaketV8
Copy link
Contributor

SaketV8 commented Jun 18, 2025

Hey @vamshi1188, I had written the following sample implementation:

  • For handling the specific case when no book is found
    I have implemented it in the GetBookByIDHandler function itself, so that the query to the DB function looks clean and we can modify our output in the handler itself.

Also,
It handles the case of an empty ID by
returning the 404 page not found if no ID is present in the URL.

And for an empty ID provided like "", it will return:

{
  "details": "sql: no rows in result set",
  "error": "Failed to get book by ID"
}

image

Thank you for bringing to my attention that I wasn’t returning the specific ID of the book that couldn’t be found

statement := `SELECT book_id, title, author, price, published_date, stock_quantity FROM books WHERE book_id = ? ;`
// Input validation
if bookID == "" {
return models.Book{}, fmt.Errorf("book ID cannot be empty")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should handle custom errors if any param is empty, maybe return a model which say things are empty so that it can be handled on the handler side

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this allows better error handling, also helps integrating a frontend(iff.) better handle more errors

Signed-off-by: vamshi <vamshigodev@gmail.com>
Signed-off-by: vamshi <vamshigodev@gmail.com>
Signed-off-by: vamshi <vamshigodev@gmail.com>
@vamshi1188
Copy link
Author

Thanks for the valuable suggestions, @SaketV8 and @khareyash05.

Here's what's now handled:

Added custom error handling for empty bookID in the repository layer and used that to return clear messages from the handler.

Improved the handler logic to return detailed errors (like sql.ErrNoRows) gracefully.

Adjusted the route to *book_id to properly catch edge cases like /id/ or missing values.

Added validations to differentiate between:

     Empty path param → returns Invalid or empty book ID

     Non-existent ID → returns Book not found

     Valid ID → returns book details

Tested all 3 scenarios via Postman, and everything works as expected now.
Screenshot from 2025-06-18 20-27-49
Screenshot from 2025-06-18 20-27-35
Screenshot from 2025-06-18 20-27-17

@vamshi1188
Copy link
Author

vamshi1188 commented Jun 19, 2025

@khareyash05 i made changes can you please review it
thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants