Skip to content

Commit

Permalink
Merge pull request #107 from collective/unify-exceptions
Browse files Browse the repository at this point in the history
Unify all exceptions raised
  • Loading branch information
maethu committed Jan 7, 2016
2 parents ab58ea4 + 7d145cf commit c0e8e05
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 34 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ Changelog
- Move inline function out of to the global scope to make it more readable.
[gforcada]

- Unify all exceptions raised by collective.solr.
[gforcada]

4.1.0 (2015-02-19)
------------------

Expand Down
6 changes: 1 addition & 5 deletions src/collective/solr/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from Acquisition import aq_base
from Missing import MV
from Products.ZCatalog.ZCatalog import ZCatalog
from collective.solr.exceptions import FallBackException
from collective.solr.interfaces import IFlare
from collective.solr.interfaces import ISearch
from collective.solr.interfaces import ISearchDispatcher
Expand All @@ -22,11 +23,6 @@
patchLazy() # ...as well as ZCatalog's Lazy class


class FallBackException(Exception):
""" exception indicating the dispatcher should fall back to searching
the portal catalog """


class SearchDispatcher(object):
""" adapter for potentially dispatching a given query to an
alternative search backend (instead of the portal catalog) """
Expand Down
25 changes: 24 additions & 1 deletion src/collective/solr/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,27 @@ class SolrException(Exception):


class SolrInactiveException(SolrException):
""" an exception indicating the solr integration is not activated """
"""An exception indicating the solr integration is not activated"""


class FallBackException(SolrException):
"""Exception indicating the dispatcher should fall back to searching
the portal catalog
"""


class SolrConnectionException(SolrException):
"""An exception thrown by solr connections"""

def __init__(self, httpcode='000', reason=None, body=None):
self.httpcode = httpcode
self.reason = reason
self.body = body

def __repr__(self):
return 'HTTP code=%s, Reason=%s, body=%s' % (
self.httpcode, self.reason, self.body
)

def __str__(self):
return 'HTTP code=%s, reason=%s' % (self.httpcode, self.reason)
10 changes: 5 additions & 5 deletions src/collective/solr/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from collective.solr.interfaces import ISolrIndexQueueProcessor
from collective.solr.interfaces import ICheckIndexable
from collective.solr.interfaces import ISolrAddHandler
from collective.solr.solr import SolrException
from collective.solr.exceptions import SolrConnectionException
from collective.solr.utils import prepareData
from socket import error
from urllib import urlencode
Expand Down Expand Up @@ -152,7 +152,7 @@ def __call__(self, conn, **data):
try:
conn.doPost(url, urlencode(postdata, doseq=True), conn.formheaders)
conn.flush()
except SolrException, e:
except SolrConnectionException, e:
logger.warn('Error %s @ %s', e, data['path_string'])
conn.reset()

Expand Down Expand Up @@ -221,7 +221,7 @@ def index(self, obj, attributes=None):
if adder is None:
adder = DefaultAdder(obj)
adder(conn, boost_values=boost_values(obj, data), **data)
except (SolrException, error):
except (SolrConnectionException, error):
logger.exception('exception during indexing %r', obj)

def reindex(self, obj, attributes=None):
Expand Down Expand Up @@ -261,7 +261,7 @@ def unindex(self, obj):
try:
logger.debug('unindexing %r (%r)', obj, data)
conn.delete(id=data_key)
except (SolrException, error):
except (SolrConnectionException, error):
logger.exception('exception during unindexing %r', obj)

def begin(self):
Expand All @@ -282,7 +282,7 @@ def commit(self, wait=None):
conn.flush()
else:
conn.commit(waitSearcher=wait)
except (SolrException, error):
except (SolrConnectionException, error):
logger.exception('exception during commit')
self.manager.closeConnection()

Expand Down
26 changes: 5 additions & 21 deletions src/collective/solr/solr.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from xml.sax.saxutils import escape
import codecs
import urllib
from collective.solr.exceptions import SolrConnectionException
from collective.solr.interfaces import ISolrConnectionConfig
from collective.solr.parser import SolrSchema
from collective.solr.timeout import HTTPConnectionWithTimeout
Expand All @@ -44,23 +45,6 @@
logger = getLogger(__name__)


class SolrException(Exception):
""" An exception thrown by solr connections """

def __init__(self, httpcode='000', reason=None, body=None):
self.httpcode = httpcode
self.reason = reason
self.body = body

def __repr__(self):
return 'HTTP code=%s, Reason=%s, body=%s' % (
self.httpcode, self.reason, self.body
)

def __str__(self):
return 'HTTP code=%s, reason=%s' % (self.httpcode, self.reason)


class SolrConnection:

def __init__(self, host='localhost:8983', solrBase='/solr',
Expand Down Expand Up @@ -110,7 +94,7 @@ def close(self):

def __errcheck(self, rsp):
if rsp.status != 200:
ex = SolrException(rsp.status, rsp.reason)
ex = SolrConnectionException(rsp.status, rsp.reason)
try:
ex.body = rsp.read()
except:
Expand Down Expand Up @@ -161,7 +145,7 @@ def flush(self):
for request in self.xmlbody:
try:
responses.append(self.doSendXML(request))
except (SolrException, socket.error):
except (SolrConnectionException, socket.error):
logger.exception('exception during request %r', request)
count += len(request)
logger.debug(
Expand All @@ -187,7 +171,7 @@ def doSendXML(self, request):
status = parsed.attrib.get('status', 0)
if status != 0:
reason = parsed.documentElement.firstChild.nodeValue
raise SolrException(rsp.status, reason)
raise SolrConnectionException(rsp.status, reason)
return parsed

def escapeVal(self, val):
Expand Down Expand Up @@ -344,4 +328,4 @@ def getSchema(self):
xml = response.read()
return SolrSchema(xml.strip())
self.__reconnect() # force a new connection for each url
self.__errcheck(response) # raise a solrexception
self.__errcheck(response) # raise a SolrConnectionException
4 changes: 2 additions & 2 deletions src/collective/solr/tests/test_facets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collective.solr.browser.facets import SearchFacetsView
from collective.solr.browser.interfaces import IThemeSpecific
from collective.solr.dispatcher import solrSearchResults
from collective.solr.solr import SolrException
from collective.solr.exceptions import SolrConnectionException
from collective.solr.testing import COLLECTIVE_SOLR_INTEGRATION_TESTING
from collective.solr.utils import activate
from plone.app.testing import TEST_USER_ID
Expand Down Expand Up @@ -190,7 +190,7 @@ def testUnknownFacetField(self):
request.form['facet'] = 'true'
request.form['facet_field'] = 'foo'
alsoProvides(request, IThemeSpecific)
self.assertRaises(SolrException, solrSearchResults, request)
self.assertRaises(SolrConnectionException, solrSearchResults, request)

def testNoFacetFields(self):
request = self.app.REQUEST
Expand Down

0 comments on commit c0e8e05

Please sign in to comment.