Skip to content

🐛 Raise descriptive error when writing failed blocks without raw bibtex - #526

Merged
MiWeiss merged 4 commits into
mainfrom
fix/400-writer-raise-without-raw
Jun 12, 2026
Merged

MiWeiss merged 4 commits into
mainfrom
fix/400-writer-raise-without-raw

Conversation

@MiWeiss

@MiWeiss MiWeiss commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

Context

Fixes the crash reported in #400: writing a library containing a ParsingFailedBlock whose raw is None (e.g. a DuplicateBlockKeyBlock produced by adding two programmatically created entries with the same key) failed with an opaque AttributeError: 'NoneType' object has no attribute 'splitlines'.

Change

The rule "failed block ⇒ write raw bibtex with a warning comment" is kept unchanged. When there is no raw bibtex, the writer no longer crashes but raises a single, actionable ValueError:

  • write() pre-scans the library and lists all offending blocks (with their errors) in one exception, so users can fix everything in one pass instead of replaying write-fail-fix.
  • The message points to library.failed_blocks and suggests removing the blocks or fixing and re-adding block.ignore_error_block.
  • _treat_failed_block keeps a backstop raise for direct callers.

Refusing to write (rather than silently falling back to serializing ignore_error_block) was discussed in #400: it loses no data, keeps the writer rule simple, and surfaces the duplicate-key bug to the user instead of shipping it downstream.

A follow-up PR proposes flipping the Library.add default to fail_on_duplicate_key=True, which would surface this class of error at the add() call site already.

🤖 Generated with Claude Code

…ex (#400)

Failed blocks (e.g. DuplicateBlockKeyBlock) created programmatically have
no raw bibtex, which previously crashed the writer with an opaque
AttributeError. The writer now pre-scans the library and raises a single
ValueError listing all such blocks, pointing users to library.failed_blocks.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@MiWeiss

MiWeiss commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator Author

reviewed

@MiWeiss
MiWeiss merged commit 1569968 into main Jun 12, 2026
19 checks passed
@MiWeiss
MiWeiss deleted the fix/400-writer-raise-without-raw branch June 12, 2026 06:01
MiWeiss added a commit that referenced this pull request Jun 12, 2026
After merging #526's tests into this branch, the duplicate must be added
with fail_on_duplicate_key=False so the test still exercises the
write-time error path.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
MiWeiss added a commit that referenced this pull request Jun 12, 2026
* 🦖 Make Library.add fail on duplicate keys by default (#400)

Programmatically adding an entry with an existing key previously replaced
it silently with a DuplicateBlockKeyBlock, surprising users at write time
(see #400). Library.add and the Library constructor now raise a ValueError
on duplicate keys by default.

Parsing behavior is unchanged: the splitter and middleware library
reconstruction explicitly pass fail_on_duplicate_key=False, so bibtex
files with duplicate keys still parse into library.failed_blocks.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* ♻️ Leave library untouched when add() raises on duplicate keys

Instead of inserting the duplicate as a DuplicateBlockKeyBlock and then
raising, add() now pre-checks all blocks (including duplicates within the
same call) and raises before any mutation, making the operation atomic.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* 📝 Align add() docstring summary with fail-by-default behavior

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* remove unneeded comment

* ✅ Adapt writer duplicate test to fail-by-default add()

After merging #526's tests into this branch, the duplicate must be added
with fail_on_duplicate_key=False so the test still exercises the
write-time error path.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant