Skip to content

Detect errors in data layer and handle them when routing #104920

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
merged 3 commits into from
Jul 28, 2025

Conversation

p-jackson
Copy link
Member

Proposed Changes

More generally, this PR suggests an error handling strategy.

  • Detect errors in the data layer
  • Handle those errors in the UI, it could be
    • router
    • suspense boundary
    • error boundary

It seems like React has moved in the direction of propagating errors through the component tree using exceptions. So perhaps we just embrace that.

More specifically, this detects and show a specific error UI when we detect the case of a Jurassic Ninja site being missing

CleanShot 2025-07-25 at 16 57 40@2x

Why make this change?

I think of the "500" error page as a last resort. It gives the user a way to recover (by navigating to /sites) but we should actually handle expected error cases with something else.

Most errors probably won't propagate all the way up to such a high level in the router. But this same exception throwing strategy should work for more localised <ErrorBoundary> components too.

Testing Instructions

Hopefully you have an old Jurassic Ninja site that has been cleaned up.
Navigate to /v2/sites/old.jurassic.ninja
You should not see the "500" error page.

Pre-merge Checklist

  • Has the general commit checklist been followed? (PCYsg-hS-p2)
  • Have you written new tests for your changes?
  • Have you tested the feature in Simple (P9HQHe-k8-p2), Atomic (P9HQHe-jW-p2), and self-hosted Jetpack sites (PCYsg-g6b-p2)?
  • Have you checked for TypeScript, React or other console errors?
  • Have you tested accessibility for your changes? Ensure the feature remains usable with various user agents (e.g., browsers), interfaces (e.g., keyboard navigation), and assistive technologies (e.g., screen readers) (PCYsg-S3g-p2).
  • Have you used memoizing on expensive computations? More info in Memoizing with create-selector and Using memoizing selectors and Our Approach to Data
  • Have we added the "[Status] String Freeze" label as soon as any new strings were ready for translation (p4TIVU-5Jq-p2)?
    • For UI changes, have we tested the change in various languages (for example, ES, PT, FR, or DE)? The length of text and words vary significantly between languages.
  • For changes affecting Jetpack: Have we added the "[Status] Needs Privacy Updates" label if this pull request changes what data or activity we track or use (p4TIVU-aUh-p2)?

@p-jackson p-jackson requested a review from a team as a code owner July 25, 2025 05:04
@matticbot matticbot added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Jul 25, 2025
@p-jackson p-jackson self-assigned this Jul 25, 2025
Copy link

github-actions bot commented Jul 25, 2025

@@ -0,0 +1,13 @@
export class DashboardDataError extends Error {
Copy link
Member Author

Choose a reason for hiding this comment

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

By making our own type we can do instanceof DashboardDataError

@matticbot
Copy link
Contributor

matticbot commented Jul 25, 2025

This PR modifies the release build for the following Calypso Apps:

For info about this notification, see here: PCYsg-OT6-p2

  • blaze-dashboard
  • command-palette-wp-admin
  • help-center
  • notifications
  • odyssey-stats

To test WordPress.com changes, run install-plugin.sh $pluginSlug update/error-message-when-jurassic-ninja-site-gone on your sandbox.

@@ -0,0 +1,13 @@
export class DashboardDataError extends Error {
constructor(
public code: string,
Copy link
Member Author

Choose a reason for hiding this comment

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

Leaving the code as string seems ok to me. Right? We don't have a need to do exhaustive matching—we don't need to be sure the component at sites/site/error.tsx handles every since code—so I think there isn't much use in having a giant type definition that includes every single error case that every single API can return.

Throw error with generic string, catch error with generic string. Seems greppable :)

Comment on lines +10 to +11
// Fix prototype chain (important for instanceof to work reliably)
Object.setPrototypeOf( this, new.target.prototype );
Copy link
Member Author

Choose a reason for hiding this comment

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

AI suggested this. Apparently in some transpiling situations can break the prototype chain.

Comment on lines 121 to 125
} catch ( error ) {
if ( error instanceof Error && error.message.includes( '[-32710]' ) ) {
throw new DashboardDataError( 'inaccessible_jetpack', error );
}
throw error;
Copy link
Member Author

Choose a reason for hiding this comment

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

My thinking here is that the error handling is fine tuned to what v2 needs. Hence why it is a dashboard data error. We should only feel we need to detect the error situations we care about.

Comment on lines 28 to 32
let site = queryClient.getQueryData< Site >( siteBySlugQuery( siteSlug ).queryKey );
if ( ! site ) {
const allSites = queryClient.getQueryData< Site[] >( sitesQuery().queryKey );
site = allSites?.find( ( s ) => s.slug === siteSlug );
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Being careful to only try showing the display name if it happens to already be available. We don't want to cause more errors in the error handler!

Copy link
Contributor

Choose a reason for hiding this comment

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

I feel this is a bit unnecessary. If the site is down, I don't think it's that helpful to show the site name; the site URL makes more sense to highlight.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes perhaps you're right. I'm imagining that there might be other errors we could catch that are site related, that wouldn't be as catastrophic as this one. And in that case it'd be useful to salvage whatever Site data we have.

But we should cross that bridge when we come to it. In this case where the JP site has just disappeared it is a bit more catastrophic.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed in 6135394

Copy link
Contributor

@fushar fushar left a comment

Choose a reason for hiding this comment

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

Tested; the logic looks sound, thanks!

{ fields: JOINED_SITE_FIELDS, options: JOINED_SITE_OPTIONS }
);
} catch ( error ) {
if ( error instanceof Error && error.message.includes( '[-32710]' ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be more robust to check for error.error === 'parse_error' instead of checking for substring?

Image

Copy link
Contributor

Choose a reason for hiding this comment

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

Ohh I guess because there's more kind of errors that have the same parse_error category

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmmm, actually I think -32710 does just mean the same thing as parse_error.

fbhepr%2Skers%2Sjcpbz%2Sjc%2Qpbagrag%2Szh%2Qcyhtvaf%2Swrgcnpx%2Spyvrag%2Spynff.ncv%2Qreebe.cuc%3Se%3Q5o221o91%23128-og

So I'll remove the sub-string match.

It'd be nice to have a more specific error than just "parse error" tbh. What's happening here is that we're trying to call the JP remove site, the API doesn't return, and rather than interpret that as an error that the JP site wasn't available, the error that gets propagated is that the response body was empty or something. So not ideal.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 853f435

I added some ergonomics for parsing the error object, since we can't be sure what the type is.

wpcom-xhr-request throws a type called WPError—the JS equivalent of the PHP class.

const wpe = WPError( { path, method }, statusCode, body );

I made a isWpError function to give us the same ergonomics as the PHP is_wp_error function.

Copy link
Contributor

@fushar fushar left a comment

Choose a reason for hiding this comment

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

Image

@p-jackson p-jackson merged commit eae8f27 into trunk Jul 28, 2025
11 checks passed
@p-jackson p-jackson deleted the update/error-message-when-jurassic-ninja-site-gone branch July 28, 2025 05:41
@github-actions github-actions bot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Jul 28, 2025
@a8ci18n
Copy link

a8ci18n commented Jul 28, 2025

This Pull Request is now available for translation here: https://translate.wordpress.com/deliverables/19608909

Some locales (Hebrew) have been temporarily machine-translated due to translator availability. All other translations are usually ready within a few days. Untranslated and machine-translated strings will be sent for translation next Monday and are expected to be completed by the following Friday.

Thank you @p-jackson for including a screenshot in the description! This is really helpful for our translators.

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.

4 participants