Skip to content

Premium Analytics: keep HTTP status on failed API requests - #50727

Merged
chihsuan merged 10 commits into
trunkfrom
worktree-wooa7s-1770-api-error-status
Jul 27, 2026
Merged

chihsuan merged 10 commits into
trunkfrom
worktree-wooa7s-1770-api-error-status

Conversation

@chihsuan

@chihsuan chihsuan commented Jul 22, 2026 •

Copy link
Copy Markdown
Member

Fixes WOOA7S-1770

Proposed changes

Premium Analytics uses the HTTP status from failed Stats API requests to decide whether to retry and whether to show authentication or server-error states. However, @wordpress/api-fetch normally throws only the parsed response body. WordPress.com Stats errors use an { error, message } response body without a status field, so the data layer could not distinguish deterministic failures such as 401, 403, and 404 from transient failures.

This PR:

  • Adds an apiFetch middleware that preserves the HTTP status when a failed response is thrown to the data layer.
  • Updates getApiErrorCode() to support the WordPress.com { error, message } error envelope in addition to WordPress REST API error shapes.
  • Stops retrying 404 responses because repeating the request cannot change the result. For example, loading File Downloads on a Jetpack site previously issued eight requests when both the primary and comparison requests returned 404; this is reduced to two requests.
  • Removes getStatsPlanErrorReason() and the plan-specific branches from the Devices and Top Platforms widgets. A 403 unauthorized response can represent either plan gating or insufficient permissions, so treating every such response as an upgrade requirement could show misleading upgrade messaging.
  • Aligns the Storybook mocks and unit-test fixtures with the error shape returned by the Stats API.

Plan entitlement should be determined before making the request rather than inferred from an ambiguous API error. That work is tracked separately in WOOA7S-1716.

Related product discussion/links

Does this pull request change what data or activity we track or use?

No.

Testing instructions

Automated tests

cd projects/packages/premium-analytics && pnpm test — covers the new error-status middleware, the { error, message } envelope support in getApiErrorCode(), and the retry policy.

Manual: 404 retry behavior

  1. Connect a self-hosted Jetpack site and open the Premium Analytics dashboard.
  2. Open browser developer tools and select the Network panel.
  3. Visit /reports/downloads.
  4. Filter requests by stats/file-downloads.
  5. Confirm there are two 404 requests: one for the primary period and one for the comparison period. Before this change, each request was retried three times, producing eight requests in total.

Before:

Screenshot 2026-07-22 at 3 33 10 PM

After:
Screenshot 2026-07-22 at 3 35 09 PM

@chihsuan
chihsuan requested a review from a team July 22, 2026 06:42
@chihsuan
chihsuan requested a review from a team as a code owner July 22, 2026 06:42
@github-actions

github-actions Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@github-actions github-actions Bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Jul 22, 2026
@chihsuan
chihsuan marked this pull request as draft July 22, 2026 06:51
@chihsuan chihsuan self-assigned this Jul 22, 2026
@chihsuan chihsuan changed the title Stats data layer: keep HTTP status on failed API requests Premium Analytics: keep HTTP status on failed API requests Jul 22, 2026
…api-error-status

# Conflicts:
#	projects/packages/premium-analytics/widgets/devices/use-device-views.ts
#	projects/packages/premium-analytics/widgets/top-platforms/use-platform-views.ts
@chihsuan
chihsuan marked this pull request as ready for review July 22, 2026 07:47
@github-actions

github-actions Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (WordPress.com Site Helper), and enable the worktree-wooa7s-1770-api-error-status branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack-mu-wpcom-plugin worktree-wooa7s-1770-api-error-status

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@jp-launch-control

jp-launch-control Bot commented Jul 22, 2026 •

Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 7 files. Only the first 5 are listed here.

File Coverage Δ% Δ Uncovered
projects/packages/premium-analytics/packages/widgets-toolkit/src/stories/mocks/register-report-mocks.ts 0/413 (0.00%) 0.00% 2 ❤️‍🩹
projects/packages/premium-analytics/widgets/utm-insights/use-utm-insights.ts 0/8 (0.00%) 0.00% 1 ❤️‍🩹
projects/packages/premium-analytics/packages/data/src/providers/query-client-provider.tsx 13/20 (65.00%) 1.84% 0 💚
projects/packages/premium-analytics/packages/data/src/utils/api-error.ts 30/30 (100.00%) 0.00% 0 💚
projects/packages/premium-analytics/widgets/search-terms/use-search-term-views.ts 5/5 (100.00%) 0.00% 0 💚

2 files are newly checked for coverage.

File Coverage
projects/packages/premium-analytics/packages/data/src/api/error-status-middleware.ts 34/34 (100.00%) 💚
projects/packages/premium-analytics/packages/widgets-toolkit/src/helpers/describe-error.ts 3/3 (100.00%) 💚

Full summary · PHP report · JS report

@chihsuan chihsuan added [Status] Needs Review This PR is ready for review. and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Status] In Progress labels Jul 22, 2026

@layoutd layoutd 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.

I tried the 404 manual testing instructions, but kept seeing more than two requests. I saw eight in total: the primary and comparison requests were each attempted four times.

From what I can tell, this happens because WordPress 7.0.x’s initSinglePage() does not run initModules (I tested with 7.0.2). Support for that was added to WordPress 7.1/trunk in this WordPress core sync.

The new error-status middleware therefore is not registered on WordPress 7.0.x, the 404 status is lost, and React Query retries each request three times.

Should we ensure the middleware is registered when running on WordPress 7.0.x as well?

Comment thread projects/packages/premium-analytics/changelog/fix-stats-api-error-status Outdated
// so the error UI shows at once instead of after the query's retry backoff.
return Promise.reject( {
code: 'stats_mock_error',
error: 'unauthorized',

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.

Could we update the remaining error fixtures too? widgets/stories/force-stats-mock-state.ts and the two video-detail tests still use the old { code, data.status } shape instead of { error, status }.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done. force-stats-mock-state.ts and both video-detail tests now use { error, status }

chihsuan and others added 2 commits July 27, 2026 11:21
* refactor: share Stats widget error descriptions

* fix: keep no_connection 403 retryable, gate hook errors, use full-sentence retry copy

* test: add retryable error stories for the shared Stats error mapper
…ror-status

Co-authored-by: Justin P <228780+layoutd@users.noreply.github.com>
@github-actions github-actions Bot added the Docs label Jul 27, 2026
chihsuan added 3 commits July 27, 2026 12:17
WP 7.0's Core boot ignores initModules in initSinglePage(), so the app's
init() module never runs there and the middleware was not installed.
Register it next to the retry policy that reads the status instead.
…tatus' into worktree-wooa7s-1770-api-error-status

# Conflicts:
#	projects/packages/premium-analytics/widgets/search-terms/render.tsx
#	projects/packages/premium-analytics/widgets/stories/force-stats-mock-state.ts
#	projects/packages/premium-analytics/widgets/top-platforms/render.tsx
#	projects/packages/premium-analytics/widgets/utm-insights/render.tsx
@chihsuan

Copy link
Copy Markdown
Member Author

Should we ensure the middleware is registered when running on WordPress 7.0.x as well?

Good catch! #50309 is the right fix for initModules. For now, I've changed this PR to registers middleware when the data package loads.

@chihsuan
chihsuan merged commit 54880a4 into trunk Jul 27, 2026
75 checks passed
@chihsuan
chihsuan deleted the worktree-wooa7s-1770-api-error-status branch July 27, 2026 06:51
@github-actions github-actions Bot removed the [Status] Needs Review This PR is ready for review. label Jul 27, 2026
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.

2 participants