From 1fc537054bccb5069f78c5a2cc17383b172713e5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 17 Aug 2013 09:34:57 +0530 Subject: [PATCH] Content Server: Proper error message for pick a random book Raise a 404 instead of a 500 if the user tries pick a random book on an empty library. --- src/calibre/library/server/browse.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/calibre/library/server/browse.py b/src/calibre/library/server/browse.py index ef6b8f3f3c..872c0e65f8 100644 --- a/src/calibre/library/server/browse.py +++ b/src/calibre/library/server/browse.py @@ -906,8 +906,11 @@ class BrowseServer(object): @Endpoint() def browse_random(self, *args, **kwargs): import random - book_id = random.choice(self.db.search_getting_ids( - '', self.search_restriction)) + try: + book_id = random.choice(self.db.search_getting_ids( + '', self.search_restriction)) + except IndexError: + raise cherrypy.HTTPError(404, 'This library has no books') ans = self.browse_render_details(book_id, add_random_button=True) return self.browse_template('').format( title='', script='book();', main=ans)