Fix #786: Handle corrupted journal files gracefully by renaming them
Fixes an issue where RedNotebook would crash when encountering corrupted journal files containing null characters or other control characters that are invalid in YAML.
Problem
When RedNotebook encountered a corrupted journal file (e.g., one containing null bytes \x00), the YAML parser would throw a ReaderError with the message "unacceptable character #x0000: control characters are not allowed". The application would catch this exception but then call sys.exit(1), causing RedNotebook to crash completely and preventing users from accessing any of their journal entries.
Solution
This PR modifies the file loading logic to handle corrupted files gracefully:
-
Detect corrupted files: When a YAML
ReaderErrormentions control characters, the file is identified as corrupted rather than just malformed -
Rename corrupted files: Corrupted files are automatically renamed with a
_corruptedsuffix (e.g.,2024-10.txt→2024-10_corrupted.txt) - Continue loading: The application continues loading other valid journal files instead of crashing
- User notification: Users receive a clear error message explaining what happened and how to recover their data
Example
Before this fix:
ERROR: unacceptable character #x0000: control characters are not allowed
[Application crashes - user loses access to all journal entries]
After this fix:
ERROR: unacceptable character #x0000: control characters are not allowed
INFO: Renamed corrupted file 2024-10.txt to 2024-10_corrupted.txt
[Application continues normally, user sees error dialog with recovery instructions]
Changes Made
- Modified
_load_month_from_disk()instorage.pyto detect and rename corrupted files - Updated
load_all_months_from_disk()to return information about corrupted files - Added user-friendly error messages in
journal.pywith recovery instructions - Ensured backward compatibility with existing save/load functionality
- Added comprehensive test suite covering various corruption scenarios
Testing
- Tested with the actual corrupted file from the issue report
- Verified normal file operations continue to work correctly
- Added test cases for multiple corruption scenarios and edge cases
- Confirmed error messages are properly displayed to users
This change transforms a catastrophic crash into a graceful degradation with clear recovery guidance, significantly improving the user experience when dealing with data corruption.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
@copilot fix tests
@copilot fix tests
Fixed the tests by adding comprehensive GTK mocking in conftest.py and updating the journal test to work without GTK dependencies. All tests now pass (94 passed, 2 skipped) in the headless environment. Changes in commit 449d336.