Permalink
Browse files

Provide an exception when importing a backend dependency fails. Thank…

…s brosner for the initial patch.
  • Loading branch information...
1 parent 7f0b377 commit 6bb06318bcfada18c620c612bdbd2eafb5c56ca8 @toastdriven toastdriven committed Apr 18, 2009
View
2 TODO
@@ -54,4 +54,4 @@ Complete
* A means to control commit frequency on updates.
* Better automatic site loading.
* Docs on SearchIndexes (prepare's).
-
+* Handle missing dependencies.
@@ -2,16 +2,13 @@
from django.db.models.base import ModelBase
from django.utils.encoding import force_unicode
from haystack.constants import VALID_FILTERS, FILTER_SEPARATOR
+from haystack.exceptions import SearchBackendError
try:
set
except NameError:
from sets import Set as set
-class SearchBackendError(Exception):
- pass
-
-
class BaseSearchBackend(object):
"""
Abstract search engine base class.
@@ -1,10 +1,13 @@
-from pysolr import Solr
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.encoding import force_unicode
from haystack.backends import BaseSearchBackend, BaseSearchQuery
+from haystack.exceptions import MissingDependency
from haystack.models import SearchResult
-
+try:
+ from pysolr import Solr
+except ImportError:
+ raise MissingDependency("The 'solr' backend requires the installation of 'pysolr'. Please refer to the documentation.")
# Word reserved by Solr for special use.
RESERVED_WORDS = (
@@ -1,14 +1,18 @@
import os
import warnings
-from whoosh import store
-from whoosh.fields import Schema, ID, STORED, TEXT, KEYWORD
-import whoosh.index as index
-from whoosh.qparser import QueryParser
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.encoding import force_unicode
-from haystack.backends import BaseSearchBackend, BaseSearchQuery, SearchBackendError
+from haystack.backends import BaseSearchBackend, BaseSearchQuery
+from haystack.exceptions import MissingDependency, SearchBackendError
from haystack.models import SearchResult
+try:
+ from whoosh import store
+ from whoosh.fields import Schema, ID, STORED, TEXT, KEYWORD
+ import whoosh.index as index
+ from whoosh.qparser import QueryParser
+except ImportError:
+ raise MissingDependency("The 'whoosh' backend requires the installation of 'Whoosh'. Please refer to the documentation.")
# Word reserved by Whoosh for special use.
View
@@ -0,0 +1,23 @@
+class HaystackError(Exception):
+ """A generic exception for all others to extend."""
+ pass
+
+class SearchBackendError(HaystackError):
+ """Raised when a backend can not be found."""
+ pass
+
+class SearchFieldError(HaystackError):
+ """Raised when a field encounters an error."""
+ pass
+
+class MissingDependency(HaystackError):
+ """Raised when a library a backend depends on can not be found."""
+ pass
+
+class AlreadyRegistered(HaystackError):
+ """Raised when a model is already registered with a site."""
+ pass
+
+class NotRegistered(HaystackError):
+ """Raised when a model is not registered with a site."""
+ pass
View
@@ -1,8 +1,5 @@
from django.template import loader, Context
-
-
-class SearchFieldError(Exception):
- pass
+from haystack.exceptions import SearchFieldError
# All the SearchFields variants.
View
@@ -1,5 +1,6 @@
-from django.db.models import signals, loading
+from django.db.models import signals
from django.db.models.base import ModelBase
+from haystack.exceptions import AlreadyRegistered, NotRegistered
from haystack.indexes import BasicSearchIndex
from haystack.fields import *
try:
@@ -8,13 +9,6 @@
from sets import Set as set
-class AlreadyRegistered(Exception):
- pass
-
-class NotRegistered(Exception):
- pass
-
-
class SearchSite(object):
"""
Encapsulates all the indexes that should be available.

0 comments on commit 6bb0631

Please sign in to comment.