Skip to content

Commit

Permalink
Merge pull request #8094 from TomeCirun/fix-dataset-respond-200-when-…
Browse files Browse the repository at this point in the history
…solr-down

[8002]Fix dataset respond 200 when solr down
  • Loading branch information
pdelboca committed Apr 16, 2024
2 parents 18cda5e + a9aa712 commit 95da08f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions ckan/lib/search/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from ckan.lib.search.common import (
make_connection, SearchIndexError, SearchQueryError, # type: ignore
SolrConnectionError, # type: ignore
SearchError, is_available, SolrSettings, config
)
from ckan.lib.search.index import (
Expand Down
5 changes: 5 additions & 0 deletions ckan/lib/search/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import datetime
import logging
import re
from urllib3.exceptions import NewConnectionError
from typing import Any, Optional

import pysolr
Expand All @@ -29,6 +30,10 @@ class SearchQueryError(SearchError):
pass


class SolrConnectionError(NewConnectionError):
pass


DEFAULT_SOLR_URL = 'http://127.0.0.1:8983/solr/ckan'


Expand Down
6 changes: 5 additions & 1 deletion ckan/lib/search/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from ckan.common import config
from ckan.lib.search.common import (
make_connection, SearchError, SearchQueryError
make_connection, SearchError, SearchQueryError, SolrConnectionError
)
from ckan.types import Context

Expand Down Expand Up @@ -468,6 +468,10 @@ def _check_query_parser(param: str, value: Any):
"Can't determine Sort Order" in e.args[0] or \
'Unknown sort order' in e.args[0]:
raise SearchQueryError('Invalid "sort" parameter')

if "Failed to connect to server" in e.args[0]:
raise SolrConnectionError("Connection Error", message=e.args[0])

raise SearchError('SOLR returned an error running query: %r Error: %r' %
(query, e))
self.count = solr_response.hits
Expand Down
9 changes: 7 additions & 2 deletions ckan/views/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
from ckan.common import _, config, g, request
from ckan.views.home import CACHE_PARAMETERS
from ckan.lib.plugins import lookup_package_plugin
from ckan.lib.search import SearchError, SearchQueryError, SearchIndexError
from ckan.lib.search import (
SearchError, SearchQueryError, SearchIndexError, SolrConnectionError
)
from ckan.types import Context, Response


Expand Down Expand Up @@ -335,7 +337,10 @@ def search(package_type: str) -> str:
_(u'Invalid search query: {error_message}')
.format(error_message=str(se))
)
except SearchError as se:
except (SearchError, SolrConnectionError) as se:
if isinstance(se, SolrConnectionError):
base.abort(500, se.args[0])

# May be bad input from the user, but may also be more serious like
# bad code causing a SOLR syntax error, or a problem connecting to
# SOLR
Expand Down

0 comments on commit 95da08f

Please sign in to comment.