Skip to content

Map rs media upload failures to specific error types (cherry-pick #23023)#23028

Merged
adalpari merged 3 commits into
release/26.9from
cherry-pick/media-upload-error-mapping-26.9
Jun 24, 2026
Merged

Map rs media upload failures to specific error types (cherry-pick #23023)#23028
adalpari merged 3 commits into
release/26.9from
cherry-pick/media-upload-error-mapping-26.9

Conversation

@adalpari

Copy link
Copy Markdown
Contributor

Description

Cherry-pick of #23023 (8a595bb4f38) onto release/26.9. The cherry-pick applied cleanly with no conflicts.


Self-hosted sites authenticated with an Application Password upload media through the new wordpress-rs REST path (MediaRSApiRestClient). When an upload failed, four genuinely different WpRequestResult outcomes — InvalidHttpStatusCode, WpError, RequestExecutionFailed, and UnknownError — were all collapsed into a single GENERIC_ERROR with the message "Unknown error occurred". The real cause (HTTP status, error code, transport reason) only existed in a $mediaResponse log line, so user reports surfaced as an undiagnosable generic snackbar ("Couldn't complete this action").

This PR splits that catch-all into specific handling:

  • HTTP-status mapping (InvalidHttpStatusCode / WpError / UnknownError) reuses the existing MediaErrorType.fromHttpStatusCode() rather than duplicating the logic: 400 → BAD_REQUEST, 401 → AUTHORIZATION_REQUIRED, 403 → NOT_AUTHENTICATED, 404 → NOT_FOUND, 413 → REQUEST_TOO_LARGE, 5xx → SERVER_ERROR, else GENERIC_ERROR. fromHttpStatusCode was also generalized so the whole 5xx range (not just 500) maps to SERVER_ERROR.
  • Transport-reason mapping (RequestExecutionFailed) via mediaErrorTypeFromExecutionReason(): timeout → TIMEOUT, offline/SSL → CONNECTION_ERROR, forbidden → NOT_AUTHENTICATED, auth required/rejected/misconfigured → AUTHORIZATION_REQUIRED, non-existent site → NOT_FOUND. The when is intentionally exhaustive (no else) so a new upstream RequestExecutionErrorReason variant becomes a compile error here instead of silently degrading to GENERIC_ERROR.
  • Each branch now populates MediaError.statusCode and a structured logMessage (status, error code, reason, request URL and method) so future reports are self-diagnosing.
  • The new NOT_AUTHENTICATED mapping for 403 is wired through the UI layers (EditPostActivity, GutenbergKitActivity, WPMediaUtils) so those failures still show the existing no-permission message.

Because the upload snackbar text is driven by MediaError.type, this also improves the user-facing message: many failures now show an actionable message (e.g. "media too large", "you don't have permission to upload") instead of the generic one.

⚠️ This improves diagnosis and labelling of the error class; it does not by itself change whether a given upload succeeds.

Testing instructions

Covered by unit tests in MediaRsApiRestClientTest and MediaStoreTest. To verify locally:

  1. Run ./gradlew :libs:fluxc:testDebugUnitTest --tests "*.MediaRsApiRestClientTest" --tests "*.MediaStoreTest"
  • Verify all tests pass

On-device sanity check (self-hosted site logged in with an Application Password):

  • Verify uploads work

🤖 Generated with Claude Code

* Map rs media upload failures to specific error types

The Application-Password REST upload path collapsed four distinct rs
failures (HTTP status, WpError, execution failure, unknown) into a single
GENERIC_ERROR, hiding the cause and showing users an unhelpful generic
snackbar. Map by HTTP status and transport reason, and record
status/code/reason in logMessage so reports are diagnosable.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* Satisfy detekt: extract error-parsing helpers and suppress LargeClass

Split parseMediaError's network-error branches into small helper functions
to stay under the LongMethod and CyclomaticComplexity thresholds, and mark
the growing test class with @Suppress("LargeClass") to match the sibling
TaxonomyRsApiRestClientTest.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* Extract shared buildMediaError helper to dedupe error parsers

The four parse* helpers repeated the same MediaError.apply construction.
Centralize it in buildMediaError, which also unifies the nullable
statusCode handling. No behavior change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* Reuse MediaErrorType.fromHttpStatusCode and align 403 handling

Replace the duplicated private HTTP-status mapper in MediaRSApiRestClient
with the canonical MediaErrorType.fromHttpStatusCode, reconciling 403 to
NOT_AUTHENTICATED (was AUTHORIZATION_REQUIRED) and restoring 400 ->
BAD_REQUEST. Generalize fromHttpStatusCode's 500 case to the full 5xx
range and make the execution-reason mapping exhaustive so new upstream
RequestExecutionErrorReason variants surface as compile errors.

Extend NOT_AUTHENTICATED handling through EditPostActivity,
GutenbergKitActivity, and WPMediaUtils so 403s still show the
no-permission message. Add tests for the 5xx range and forbidden
execution reason.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@dangermattic

dangermattic commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator
1 Warning
⚠️ PR is not assigned to a milestone.

Generated by 🚫 Danger

@wpmobilebot

wpmobilebot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

App Icon📲 You can test the changes from this Pull Request in WordPress Android by scanning the QR code below to install the corresponding build.

App NameWordPress Android
Build TypeDebug
Versionpr23028-ab5cbfa
Build Number1497
Application IDorg.wordpress.android.prealpha
Commitab5cbfa
Installation URL099dr1rvdfl90
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@wpmobilebot

wpmobilebot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

App Icon📲 You can test the changes from this Pull Request in Jetpack Android by scanning the QR code below to install the corresponding build.

App NameJetpack Android
Build TypeDebug
Versionpr23028-ab5cbfa
Build Number1497
Application IDcom.jetpack.android.prealpha
Commitab5cbfa
Installation URL2uh4o6p4e2jtg
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@codecov

codecov Bot commented Jun 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 63.76812% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 37.56%. Comparing base (c50ed4a) to head (ab5cbfa).
⚠️ Report is 1 commits behind head on release/26.9.

Files with missing lines Patch % Lines
...c/network/rest/wpapi/media/MediaRSApiRestClient.kt 62.12% 19 Missing and 6 partials ⚠️
Additional details and impacted files
@@               Coverage Diff                @@
##           release/26.9   #23028      +/-   ##
================================================
+ Coverage         37.54%   37.56%   +0.01%     
================================================
  Files              2325     2325              
  Lines            125264   125324      +60     
  Branches          17173    17187      +14     
================================================
+ Hits              47029    47073      +44     
- Misses            74421    74433      +12     
- Partials           3814     3818       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wpmobilebot

Copy link
Copy Markdown
Contributor

🤖 Build Failure Analysis

This build has failures. Claude has analyzed them - check the build annotations for details.

@adalpari adalpari requested a review from nbradbury June 24, 2026 07:39

@nbradbury nbradbury left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

:shipit:

@adalpari adalpari enabled auto-merge (squash) June 24, 2026 11:18
@adalpari adalpari merged commit 43849a5 into release/26.9 Jun 24, 2026
21 of 23 checks passed
@adalpari adalpari deleted the cherry-pick/media-upload-error-mapping-26.9 branch June 24, 2026 11:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants