John151 / readinglist

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fix crash when adding duplicate book #21

John151 opened this issue · comments

Program crashes when two books with the same author and title are added

To reproduce

Select option 1 to add a book. Enter book information
Select option 1 to add another book. Enter the same book information as before

Program crashes with the following stack trace

Traceback (most recent call last):
File "main.py", line 74, in
main()
File "main.py", line 16, in main
action()
File "main.py", line 36, in add_book
new_book.save()
File ".../python/readinglist/bookstore.py", line 24, in save
self.bookstore._add_book(self)
File ".../python/readinglist/bookstore.py", line 85, in _add_book
raise BookError(f'This book is already in the store. {book}')
bookstore.BookError: This book is already in the store. ID None, Title: E, Author: E. You have not read this book.

Suggest adding try-except block in main.py to catch the exception, and print a useful message to the user.