Skip to content

fix(card-browser): render code blocks in error messages#21003

Open
DebugWithAryan wants to merge 3 commits intoankidroid:mainfrom
DebugWithAryan:main
Open

fix(card-browser): render code blocks in error messages#21003
DebugWithAryan wants to merge 3 commits intoankidroid:mainfrom
DebugWithAryan:main

Conversation

@DebugWithAryan
Copy link
Copy Markdown

@DebugWithAryan DebugWithAryan commented May 8, 2026

Screenshot 2026-05-09 at 1 04 05 AM

Purpose / Description

When a card-browser search produces an error wrapped in <pre>...</pre>
by the Anki backend (e.g. re:b[ triggers a regex parse error), the
error dialog displayed the literal <pre> and </pre> tags instead
of rendering the code block. Anki Desktop renders these via Qt's HTML
support; AnkiDroid uses a plain TextView, so the markup leaked
through.

Fixes

Approach

  • Add formatErrorMessage(message, codeBackgroundColor) in
    com.ichi2.utils that strips <pre> tags and applies
    TypefaceSpan("monospace") + BackgroundColorSpan to the inner
    content.
  • Add a Context.formatErrorMessage(message) extension that resolves
    the code-block background from ?attr/colorSurfaceContainerHighest
    via the existing Themes.getColorFromAttr helper, so it adapts to
    light/dark themes.
  • Call it from Context.showError so every error dialog benefits.
  • Whitespace inside the <pre> block is preserved so caret indicators
    (e.g. ^ under the offending character of a regex) stay aligned.

How Has This Been Tested?

  • New unit tests in ErrorMessageFormatterTest:
    • plain message passes through unchanged
    • <pre>regex parse error: …</pre> becomes plain text with a
      TypefaceSpan("monospace") over the code range
  • Manually verified by searching re:b[ and and in the card browser
    on a debug build.

Checklist

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

Errors from the Anki backend can wrap content in <pre>...</pre> for
monospace presentation (e.g. regex parse errors). AnkiDroid's plain
TextView showed the literal tags. Strip them and apply a monospace
TypefaceSpan + a theme-aware BackgroundColorSpan so the content reads
as a code block.

Closes ankidroid#21001
@welcome
Copy link
Copy Markdown

welcome Bot commented May 8, 2026

First PR! 🚀 We sincerely appreciate that you have taken the time to propose a change to AnkiDroid! Please have patience with us as we are all volunteers - we will get to this as soon as possible.

@ZornHadNoChoice
Copy link
Copy Markdown
Collaborator

The grey bar is an Emulator bug according to David, but what about the background color of the "code" text? I'm not a fan.

@DebugWithAryan
Copy link
Copy Markdown
Author

DebugWithAryan commented May 8, 2026

Yes, I have removed that text highlighted background @ZornHadNoChoice

Errors from the Anki backend can wrap content in <pre>...</pre> for
monospace presentation (e.g. regex parse errors). AnkiDroid's plain
TextView showed the literal tags. Strip them and apply a monospace
TypefaceSpan so the content reads as a code block.

Closes ankidroid#21001
@DebugWithAryan
Copy link
Copy Markdown
Author

@ZornHadNoChoice guide me if any changes required

@ZornHadNoChoice
Copy link
Copy Markdown
Collaborator

Visually, it looks fine (aside from the emulator bug). You have to wait for the devs to comment on the code.

@DebugWithAryan
Copy link
Copy Markdown
Author

Visually, it looks fine (aside from the emulator bug). You have to wait for the devs to comment on the code.

Ok thank you

@criticalAY
Copy link
Copy Markdown
Contributor

criticalAY commented May 8, 2026

Can you confirm you didn't use an LLM to write this? (I can see LLM usage in description)

We want new contributors to gain hands-on experience with our code. While LLMs are efficient, using them for beginner-level issues takes away the chance for developers to learn by doing.

https://github.com/ankidroid/Anki-Android/blob/main/AI_POLICY.md#new-contributors

@criticalAY criticalAY added the Needs Author Reply Waiting for a reply from the original author label May 8, 2026
@DebugWithAryan
Copy link
Copy Markdown
Author

Can you confirm you didn't use an LLM to write this? (I can see LLM usable in description)

We want new contributors to gain hands-on experience with our code. While LLMs are efficient, using them for beginner-level issues takes away the chance for developers to learn by doing.

https://github.com/ankidroid/Anki-Android/blob/main/AI_POLICY.md#new-contributors

No I haven't used LLM for description I just checked other PR's for description idea as I'm new to this.

Copy link
Copy Markdown
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should match Anki Desktop here and let them fix the output.

Only handle backticks for now, as that's known to be handled upstream

https://forums.ankiweb.net/t/bug-regex-parse-error-dialog/69660

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Author Reply Waiting for a reply from the original author Needs Review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Render code blocks in error messages

4 participants