From ad953dc18ddf264fecf3d92046ac445bb334ec13 Mon Sep 17 00:00:00 2001 From: Will Kahn-Greene Date: Wed, 9 Nov 2011 09:44:33 -0500 Subject: [PATCH] Wrap excerpt in try/except excerpt can kick up socket.error and socket.timeout. So we wrap it and throw ExcerptErrors if things go awry. --- oedipus/__init__.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/oedipus/__init__.py b/oedipus/__init__.py index ff277bc..9e320be 100644 --- a/oedipus/__init__.py +++ b/oedipus/__init__.py @@ -310,9 +310,11 @@ def excerpt(self, result): """Take a result and return the excerpt as a list of unicodes--one for each highlight_field in the order specified. - :raises ExcerptError: Raises an ``ExcerptError`` if ``excerpt`` - was called before results were calculated or if - ``highlight_fields`` is not a subset of ``fields``. + :raises ExcerptError: Raises an ``ExcerptError`` if + ``excerpt`` was called before results were calculated, if + ``highlight_fields`` is not a subset of ``fields``, or if + there was a socket.error or socket.timeout trying to + retrieve the excerpt. """ # This catches the case where results haven't been calculated. @@ -348,8 +350,16 @@ def excerpt(self, result): options[mem] = getattr(self.meta, 'excerpt_' + mem) sphinx = self._sphinx() - excerpt = sphinx.BuildExcerpts( - list(docs), self.meta.index, self._query, options) + + try: + excerpt = sphinx.BuildExcerpts( + list(docs), self.meta.index, self._query, options) + except socket.error, msg: + # The sphinxapi exceptions suck, so raising our own and + # ignoring theirs doesn't make a big difference. + raise ExcerptError('Socket error building excerpt: %s!', msg) + except socket.timeout: + raise ExcerptError('Socket timeout error with excerpt!') # TODO: This assumes the data is in utf-8 which it might not # be depending on the backing database configuration.