Repository navigation
Premium Analytics: share Stats widget error descriptions #50729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chihsuan
merged 3 commits into
worktree-wooa7s-1770-api-error-status
from
wooa7s-1649-describe-error-dependent
Jul 27, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: patch | ||
| Type: changed | ||
|
|
||
| Use consistent access and retry error states across Stats widgets. |
66 changes: 66 additions & 0 deletions
66
...s/premium-analytics/packages/widgets-toolkit/src/helpers/__tests__/describe-error.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { describeError } from '../describe-error'; | ||
|
|
||
| const RETRY_DESCRIPTION = "We couldn't load device data. Please try again in a moment."; | ||
|
|
||
| describe( 'describeError', () => { | ||
| it( 'describes a 403 as a neutral error without actions', () => { | ||
| const descriptor = describeError( | ||
| { error: 'unauthorized', status: 403 }, | ||
| { retryDescription: RETRY_DESCRIPTION, onRetry: jest.fn() } | ||
| ); | ||
|
|
||
| expect( descriptor ).toEqual( { | ||
| description: "You don't have access to this data.", | ||
| } ); | ||
| expect( descriptor ).not.toHaveProperty( 'actions' ); | ||
| } ); | ||
|
|
||
| it( 'keeps a no_connection 403 retryable — a broken connection can heal', () => { | ||
| const onRetry = jest.fn(); | ||
| const descriptor = describeError( | ||
| { error: 'no_connection', status: 403 }, | ||
| { retryDescription: RETRY_DESCRIPTION, onRetry } | ||
| ); | ||
|
|
||
| expect( descriptor.description ).toBe( RETRY_DESCRIPTION ); | ||
| expect( descriptor.actions ).toEqual( [ { label: 'Retry', onClick: onRetry } ] ); | ||
| } ); | ||
|
|
||
| it( 'describes a generic failure with the retryable copy and a retry action', () => { | ||
| const onRetry = jest.fn(); | ||
| const descriptor = describeError( | ||
| { status: 500 }, | ||
| { retryDescription: RETRY_DESCRIPTION, onRetry } | ||
| ); | ||
|
|
||
| expect( descriptor.description ).toBe( RETRY_DESCRIPTION ); | ||
| expect( descriptor.actions ).toEqual( [ { label: 'Retry', onClick: onRetry } ] ); | ||
| } ); | ||
|
|
||
| it( 'keeps an unexpected 404 retryable', () => { | ||
| const onRetry = jest.fn(); | ||
| const descriptor = describeError( | ||
| { status: 404 }, | ||
| { retryDescription: RETRY_DESCRIPTION, onRetry } | ||
| ); | ||
|
|
||
| expect( descriptor.actions ).toEqual( [ { label: 'Retry', onClick: onRetry } ] ); | ||
| } ); | ||
|
|
||
| it( 'returns the retryable descriptor when there is no error — widgets call it on every render', () => { | ||
| const onRetry = jest.fn(); | ||
|
|
||
| for ( const noError of [ null, undefined ] ) { | ||
| const descriptor = describeError( noError, { | ||
| retryDescription: RETRY_DESCRIPTION, | ||
| onRetry, | ||
| } ); | ||
|
|
||
| expect( descriptor.description ).toBe( RETRY_DESCRIPTION ); | ||
| expect( descriptor.actions ).toEqual( [ { label: 'Retry', onClick: onRetry } ] ); | ||
| } | ||
| } ); | ||
| } ); |
51 changes: 51 additions & 0 deletions
51
projects/packages/premium-analytics/packages/widgets-toolkit/src/helpers/describe-error.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { getApiErrorCode, getApiErrorStatus } from '@jetpack-premium-analytics/data'; | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| /** | ||
| * Types | ||
| */ | ||
| import type { WidgetStateError } from '../components/widget-state'; | ||
|
|
||
| interface DescribeErrorOptions { | ||
| retryDescription: string; | ||
| onRetry: () => void; | ||
| } | ||
|
|
||
| /** | ||
| * Map an API error to a Stats widget error descriptor. | ||
| * | ||
| * A 403 is a deterministic access failure, so it gets neutral copy and no retry | ||
| * action — except the proxy's `no_connection` 403, which flags a broken Jetpack | ||
| * connection that can heal, so it stays retryable like any other failure. | ||
| * | ||
| * @param error - The failed query error. | ||
| * @param options - Error-state copy and retry options. | ||
| * @param options.retryDescription - The full-sentence copy for the retryable error state. | ||
| * @param options.onRetry - Callback used by the retry action. | ||
| * @return The widget error descriptor. | ||
| */ | ||
| export function describeError( | ||
| error: unknown, | ||
| { retryDescription, onRetry }: DescribeErrorOptions | ||
| ): WidgetStateError { | ||
| if ( getApiErrorStatus( error ) === 403 && getApiErrorCode( error ) !== 'no_connection' ) { | ||
| return { | ||
| description: __( "You don't have access to this data.", 'jetpack-premium-analytics' ), | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| description: retryDescription, | ||
| actions: [ | ||
| { | ||
| label: __( 'Retry', 'jetpack-premium-analytics' ), | ||
| onClick: onRetry, | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking: The Storybook error mocks return a 403, so these stories no longer show Retry as their descriptions claim. Maybe update the descriptions and add a retryable
500/no_connectionstory?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — confirmed. Both mock helpers reject with a 403 whose code isn't
no_connection, so those four stories were rendering the neutral no-access copy while their descriptions still promised a Retry.Fixed in edc2563