Skip to content

Print WebView: surface main-frame load errors with a toast#365

Open
jim-daf wants to merge 1 commit into
FossifyOrg:mainfrom
jim-daf:webview-print-onreceivederror
Open

Print WebView: surface main-frame load errors with a toast#365
jim-daf wants to merge 1 commit into
FossifyOrg:mainfrom
jim-daf:webview-print-onreceivederror

Conversation

@jim-daf
Copy link
Copy Markdown

@jim-daf jim-daf commented May 20, 2026

Closes #364.

The print flow uses a hidden WebView whose WebViewClient only overrides shouldOverrideUrlLoading and onPageFinished. onPageFinished is what kicks off createWebPrintJob, so if the underlying loadData fails (large note, transient renderer error, etc.) the print silently never starts and the user gets no feedback at all.

This PR adds onReceivedError on the same anonymous WebViewClient:

override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) {
    super.onReceivedError(view, request, error)
    if (request.isForMainFrame) {
        toast(org.fossify.commons.R.string.unknown_error_occurred)
    }
}

Why this shape:

  • request.isForMainFrame guards against firing the toast twice if any sub-resource also fails (the WebView is loading text via loadData, so sub-resources should not really exist, but the guard keeps the override future-proof).
  • The fully qualified org.fossify.commons.R.string.unknown_error_occurred is reused so the message stays already translated. The same string is used elsewhere in this activity from showErrorToast, which keeps the failure UX consistent with the rest of the print pipeline.
  • Only one new import (android.webkit.WebResourceError). No new permission, no new resource, no behaviour change on successful prints.

The hidden WebView built in printText() drives the print flow via
onPageFinished -> createWebPrintJob. The WebViewClient does not override
onReceivedError, so if loadData() fails for any reason (very large note
that exceeds WebView limits, transient renderer error, etc.) the user
gets nothing - no toast, no log, and the print never starts because
onPageFinished is also not called.

Add onReceivedError on the same anonymous WebViewClient. Guard with
request.isForMainFrame so any future sub-resource error does not toast
twice, and reuse the existing commons string unknown_error_occurred so
the message is already translated.
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.

Print WebView swallows main-frame load errors

1 participant