diff --git a/api/views.py b/api/views.py index a8d60b4662..b91fa760c4 100644 --- a/api/views.py +++ b/api/views.py @@ -1,7 +1,7 @@ from sefaria.model import * from sefaria.model.text_reuqest_adapter import TextRequestAdapter from sefaria.client.util import jsonResponse -from sefaria.system.exceptions import InputError +from sefaria.system.exceptions import InputError, ComplexBookLevelRefError from django.views import View from .api_warnings import * @@ -59,12 +59,16 @@ def get(self, request, *args, **kwargs): try: data = text_manager.get_versions_for_query() - except InputError as e: - if str(e) == "Can not get TextRange at this level, please provide a more precise reference": - return jsonResponse({'error': "Please pass a more specific ref for this book, and try again. The ref you passed is a \'complex\' book-level ref. We only support book-level refs in cases of texts with a 'simple' structure. To learn more about the structure of a text on Sefaria, see: https://developers.sefaria.org/docs/the-schema-of-a-simple-text"}, status=400) - data = self._handle_warnings(data) + data = self._handle_warnings(data) + + except Exception as e: + if isinstance(e, InputError): + return jsonResponse({'error': e.message}) + return jsonResponse(data) + + diff --git a/reader/views.py b/reader/views.py index 527a185afb..f3fae99373 100644 --- a/reader/views.py +++ b/reader/views.py @@ -1450,7 +1450,7 @@ def _get_text(oref, versionEn=versionEn, versionHe=versionHe, commentary=comment try: text = _get_text(oref, versionEn=versionEn, versionHe=versionHe, commentary=commentary, context=context, pad=pad, alts=alts, wrapLinks=wrapLinks, layer_name=layer_name) - except Exception as e: + except InputError as e: if isinstance(e, ComplexBookLevelRefError): return jsonResponse({'error': e.message}, status=400) return jsonResponse(text, cb) diff --git a/sefaria/model/text.py b/sefaria/model/text.py index f9bea61dae..d9c78cc652 100644 --- a/sefaria/model/text.py +++ b/sefaria/model/text.py @@ -25,7 +25,7 @@ import sefaria.system.cache as scache from sefaria.system.cache import in_memory_cache from sefaria.system.exceptions import InputError, BookNameError, PartialRefInputError, IndexSchemaError, \ - NoVersionFoundError, DictionaryEntryNotFoundError, MissingKeyError + NoVersionFoundError, DictionaryEntryNotFoundError, MissingKeyError, ComplexBookLevelRefError from sefaria.utils.hebrew import has_hebrew, is_all_hebrew, hebrew_term from sefaria.utils.util import list_depth, truncate_string from sefaria.datatype.jagged_array import JaggedTextArray, JaggedArray @@ -1693,7 +1693,7 @@ def __init__(self, oref, lang, vtitle, merge_versions=False, versions=None): elif oref.has_default_child(): #use default child: self.oref = oref.default_child_ref() else: - raise InputError("Can not get TextRange at this level, please provide a more precise reference") + raise ComplexBookLevelRefError(book_ref=oref.normal()) self.lang = lang self.vtitle = vtitle self.merge_versions = merge_versions @@ -2424,7 +2424,7 @@ def __init__(self, oref, context=1, commentary=True, version=None, lang=None, self._alts = [] if not isinstance(oref.index_node, JaggedArrayNode) and not oref.index_node.is_virtual: - raise InputError("Can not get TextFamily at this level, please provide a more precise reference") + raise InputError("Unable to find text for that ref") for i in range(0, context): oref = oref.context_ref() @@ -3991,7 +3991,7 @@ def padded_ref(self): if self.has_default_child(): return self.default_child_ref().padded_ref() elif self.is_book_level(): - raise InputError("Can not get TextRange at this level, please provide a more precise reference") + raise ComplexBookLevelRefError(book_ref=self.normal()) else: raise InputError("Cannot pad a schema node ref.") diff --git a/sefaria/system/exceptions.py b/sefaria/system/exceptions.py index d0029d2105..8ef21b8f59 100644 --- a/sefaria/system/exceptions.py +++ b/sefaria/system/exceptions.py @@ -100,8 +100,12 @@ def __init__(self, method): self.message = f"'{method}' is not a valid HTTP API method." super().__init__(self.message) -class ComplexBookLevelRefError(Exception): +class ComplexBookLevelRefError(InputError): def __init__(self, book_ref): self.book_ref = book_ref - self.message = f"Please pass a more specific ref for this book, and try again. You passed in a \'complex\' book-level ref. We only support book-level refs in cases of texts with a 'simple' structure. To learn more about the structure of a text on Sefaria, see: https://developers.sefaria.org/docs/the-schema-of-a-simple-text" + self.message = (f"You passed '{book_ref}', please pass a more specific ref for this book, and try again. " + f"'{book_ref}' is a \'complex\' book-level ref. We only support book-level " + f"refs in cases of texts with a 'simple' structure. To learn more about the " + f"structure of a text on Sefaria, " + f"see: https://developers.sefaria.org/docs/the-schema-of-a-simple-text") super().__init__(self.message)