Skip to content

fix: fall back when a declared charset is unknown - #192

Open
Muneerali199 wants to merge 1 commit into
AOSSIE-Org:mainfrom
Muneerali199:fix/unknown-charset-fallback
Open

Muneerali199 wants to merge 1 commit into
AOSSIE-Org:mainfrom
Muneerali199:fix/unknown-charset-fallback

Conversation

@Muneerali199

@Muneerali199 Muneerali199 commented Nov 21, 2026 •

Copy link
Copy Markdown

Fixes #190

bytes.decode() raises LookupError for an unknown charset label, so the UnicodeDecodeError fallback in _decode never ran and the error escaped article extraction. Unknown charsets now fall through to the UTF-8 then Latin-1 decoding path. Added a regression test using charset=utf-8-bogus.

Verification: the focused decode path was exercised locally; the full backend suite cannot run on this macOS x86_64 host because the locked torch version has no compatible wheel.

Summary by CodeRabbit

  • Bug Fixes
    • Responses with unrecognized character encodings now fall back to alternative decoding, helping preserve readable text.

bytes.decode raises LookupError (not UnicodeDecodeError) for an unrecognized charset label, bypassing the UTF-8/Latin-1 fallback. Catch it and use the normal fallback instead. Added a regression test.

Fixes AOSSIE-Org#190

Signed-off-by: Muneerali199 <alimuneerali245@gmail.com>
@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The scraper now handles unrecognized response charset labels by continuing to its existing UTF-8 and Latin-1 fallback. A regression test checks the result for Latin-1 response bytes.

Changes

Unknown Charset Fallback

Layer / File(s) Summary
Decode fallback and regression test
backend/app/modules/scraper/safe_fetch.py, backend/tests/test_safe_fetch.py
_decode catches LookupError for an unrecognized declared charset and continues to the existing decoding fallback. The test checks that Latin-1 response bytes produce the expected text.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested labels: Backend, Tests

Suggested reviewers: reach2saksham

Merge Risk: Minimal · up to 05165

Pages that declare an unknown charset label are now decoded with the existing fallback instead of raising an error during article extraction. The change is narrow and has a regression test, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix: fall back when a declared charset is unknown" matches the changeset. The change makes _decode in backend/app/modules/scraper/safe_fetch.py catch LookupError for unrecognized charset la…
Linked Issues check Passed Issue #190 requires that an unknown charset label not raise LookupError during article extraction. Its expected behavior is the UTF-8-then-Latin-1 fallback, or the normal "Failed to extract article." …
Out of Scope Changes check Passed The PR changes two files. In safe_fetch.py, the change is limited to the LookupError handler in _decode, which directly implements #190. In test_safe_fetch.py, the change adds one regression t…
Full details: Title check

Explanation

The title "fix: fall back when a declared charset is unknown" matches the changeset. The change makes _decode in backend/app/modules/scraper/safe_fetch.py catch LookupError for unrecognized charset labels and continue to the existing UTF-8 and Latin-1 fallback. A regression test in backend/tests/test_safe_fetch.py covers an unknown charset with Latin-1 bytes. The title names the main behavior change and is specific enough for a teammate scanning history.

Full details: Linked Issues check

Explanation

Issue #190 requires that an unknown charset label not raise LookupError during article extraction. Its expected behavior is the UTF-8-then-Latin-1 fallback, or the normal "Failed to extract article." result. It also asks for a regression test for an unknown charset label.

At head, _decode in backend/app/modules/scraper/safe_fetch.py wraps body.decode(res.encoding, errors="replace") in try/except LookupError and then falls through to body.decode("utf-8") with a UnicodeDecodeError fallback to latin-1. An unknown label now takes the same path as an undeclared charset, so the LookupError no longer reaches Article_extractor._fetch_html.

backend/tests/test_safe_fetch.py adds test_unknown_charset_falls_back_to_utf8_then_latin1. It uses Content-Type: text/html; charset=utf-8-bogus with Latin-1 bytes for "café" and asserts the decoded result is "café". This is the regression test the issue requests.

The issue's FastAPI HTTP status is not a stated requirement, and the issue states it was not verified. I did not assess it. I did not run the test suite. The repository diff tool was unavailable, so I verified the change from the head file contents, not from a diff.

Full details: Out of Scope Changes check

Explanation

The PR changes two files. In safe_fetch.py, the change is limited to the LookupError handler in _decode, which directly implements #190. In test_safe_fetch.py, the change adds one regression test for the same unknown-charset path. The PR summary reports +6/-1 and +13/-0 lines, which matches this scope. I found no unrelated changes in the inspected head contents. The repository diff tool was unavailable, so I could not check the complete diff against the merge base line by line. This limit does not change the verdict because the head file contents match the described change.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checked the charset name,
Then watched the fallback do its work.
UTF-8 tried, Latin-1 came through,
The page text reached the test at last.
The rabbit nibbled, pleased and calm.

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 144a9d3 and 051650a.

Files selected for processing: 2

  • backend/app/modules/scraper/safe_fetch.py
  • backend/tests/test_safe_fetch.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

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.

[BUG]: A page with an unknown charset label raises LookupError instead of falling back to the extraction error

1 participant