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.
This commit is contained in:
Kovid Goyal 2013-08-17 09:34:57 +05:30
parent 811b210303
commit 1fc537054b

View File

@ -906,8 +906,11 @@ class BrowseServer(object):
@Endpoint() @Endpoint()
def browse_random(self, *args, **kwargs): def browse_random(self, *args, **kwargs):
import random import random
book_id = random.choice(self.db.search_getting_ids( try:
'', self.search_restriction)) 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) ans = self.browse_render_details(book_id, add_random_button=True)
return self.browse_template('').format( return self.browse_template('').format(
title='', script='book();', main=ans) title='', script='book();', main=ans)