Skip to content

Commit

Permalink
fix: skip with an error instead of crashing when note parse failed
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhd1701 committed Jun 14, 2022
1 parent 2b6060a commit e2cd4a7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 7 additions & 3 deletions enex2notion/cli_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ def upload_note(self, note: EvernoteNote, note_idx: int):
if self.rules.tag and self.rules.tag not in note.tags:
note.tags.append(self.rules.tag)

logger.debug(f"Parsing note '{note.title}'")

note_blocks = self._parse_note(note)
if not note_blocks:
logger.debug(f"Skipping note '{note.title}' (no blocks)")
return

if self.notebook_root is not None:
Expand All @@ -79,9 +82,10 @@ def upload_note(self, note: EvernoteNote, note_idx: int):
def _parse_note(self, note):
try:
return parse_note(note, self.rules)
except Exception:
logger.error(f"Unhandled exception while parsing note '{note.title}'!")
raise
except Exception as e:
logger.error(f"Failed to parse note '{note.title}'")
logger.debug(e, exc_info=e)
return []

def _get_notebook_root(self, notebook_title):
if self.import_root is None:
Expand Down
8 changes: 3 additions & 5 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,14 @@ def test_webclip_pdf_with_preview(mock_api, fake_note_factory, mocker, parse_rul
mock_api["parse_note"].assert_called_once_with(mocker.ANY, parse_rules)


def test_unhandled_exception(mock_api, fake_note_factory, caplog):
def test_parse_exception(mock_api, fake_note_factory, caplog):
fake_exception = Exception("fake")
mock_api["parse_note"].side_effect = fake_exception

with caplog.at_level(logging.ERROR, logger="enex2notion"):
with pytest.raises(Exception) as e:
cli(["fake.enex"])
cli(["fake.enex"])

assert e.value == fake_exception
assert "Unhandled exception while parsing note" in caplog.text
assert "Failed to parse note" in caplog.text


def test_file_log(mock_api, fake_note_factory, fs):
Expand Down

0 comments on commit e2cd4a7

Please sign in to comment.