Skip to content

Commit 6b96624

Browse files
tomrod10Tomas RodriguezTomas Rodriguezpre-commit-ci[bot]cdrini
authored
Add descriptive error for malformed TOC entry (#10574)
* create TocParseError and throw error when toc values don't match their expected type --------- Co-authored-by: Tomas Rodriguez <[email protected]> Co-authored-by: Tomas Rodriguez <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Drini Cami <[email protected]>
1 parent 3153a88 commit 6b96624

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

openlibrary/plugins/upstream/addbook.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from openlibrary.plugins.upstream import spamcheck, utils
2424
from openlibrary.plugins.upstream.account import as_admin
2525
from openlibrary.plugins.upstream.models import Author, Edition, Work
26+
from openlibrary.plugins.upstream.table_of_contents import TocParseError
2627
from openlibrary.plugins.upstream.utils import fuzzy_find, render_template
2728
from openlibrary.plugins.worksearch.search import get_solr
2829

@@ -644,7 +645,12 @@ def save(self, formdata: web.Storage) -> None:
644645
edition_data.pop('physical_dimensions', None)
645646
)
646647
self.edition.set_weight(edition_data.pop('weight', None))
647-
self.edition.set_toc_text(edition_data.pop('table_of_contents', None))
648+
try:
649+
self.edition.set_toc_text(edition_data.pop('table_of_contents', None))
650+
except TocParseError as e:
651+
raise ClientException(
652+
"400 Bad Request", f"Table of contents parse error: {e}"
653+
)
648654

649655
if edition_data.pop('translation', None) != 'yes':
650656
edition_data.translation_of = None

openlibrary/plugins/upstream/table_of_contents.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class AuthorRecord(TypedDict, total=False):
6464
author: ThingReferenceDict | None
6565

6666

67+
class TocParseError(BaseException):
68+
pass
69+
70+
6771
@dataclass
6872
class TocEntry:
6973
level: int
@@ -120,6 +124,7 @@ def from_markdown(line: str) -> 'TocEntry':
120124
>>> f("1.1 | Apple")
121125
(0, '1.1', 'Apple', None)
122126
"""
127+
123128
RE_LEVEL = web.re_compile(r"(\**)(.*)")
124129
level, text = RE_LEVEL.match(line.strip()).groups()
125130

@@ -130,13 +135,20 @@ def from_markdown(line: str) -> 'TocEntry':
130135
title = text
131136
label = page = ""
132137
extra_fields = ''
138+
try:
139+
extra_fields = json.loads(extra_fields or '{}')
140+
except json.JSONDecodeError as e:
141+
raise TocParseError(f"Invalid JSON: {e}")
142+
143+
if not isinstance(extra_fields, dict):
144+
raise TocParseError("Invalid formatting!")
133145

134146
return TocEntry(
135147
level=len(level),
136148
label=label.strip() or None,
137149
title=title.strip() or None,
138150
pagenum=page.strip() or None,
139-
**json.loads(extra_fields or '{}'),
151+
**extra_fields,
140152
)
141153

142154
def to_markdown(self) -> str:

0 commit comments

Comments
 (0)