Skip to content

Commit

Permalink
chore(api errors): Raise exception, find the right place in model for…
Browse files Browse the repository at this point in the history
… exception
  • Loading branch information
saengel committed Nov 13, 2024
1 parent 25fc731 commit 599180d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
14 changes: 9 additions & 5 deletions api/views.py
Original file line number Diff line number Diff line change
@@ -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 *

Expand Down Expand Up @@ -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)






2 changes: 1 addition & 1 deletion reader/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions sefaria/model/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.")

Expand Down
8 changes: 6 additions & 2 deletions sefaria/system/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 599180d

Please sign in to comment.