-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Detect errors in data layer and handle them when routing #104920
Conversation
Jetpack Cloud live (direct link)
Automattic for Agencies live (direct link)
|
@@ -0,0 +1,13 @@ | |||
export class DashboardDataError extends Error { |
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.
By making our own type we can do instanceof DashboardDataError
This PR modifies the release build for the following Calypso Apps: For info about this notification, see here: PCYsg-OT6-p2
To test WordPress.com changes, run |
@@ -0,0 +1,13 @@ | |||
export class DashboardDataError extends Error { | |||
constructor( | |||
public code: string, |
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.
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 :)
// Fix prototype chain (important for instanceof to work reliably) | ||
Object.setPrototypeOf( this, new.target.prototype ); |
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.
AI suggested this. Apparently in some transpiling situations can break the prototype chain.
client/dashboard/data/site.ts
Outdated
} catch ( error ) { | ||
if ( error instanceof Error && error.message.includes( '[-32710]' ) ) { | ||
throw new DashboardDataError( 'inaccessible_jetpack', error ); | ||
} | ||
throw error; |
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.
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.
let site = queryClient.getQueryData< Site >( siteBySlugQuery( siteSlug ).queryKey ); | ||
if ( ! site ) { | ||
const allSites = queryClient.getQueryData< Site[] >( sitesQuery().queryKey ); | ||
site = allSites?.find( ( s ) => s.slug === siteSlug ); | ||
} |
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.
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!
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.
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.
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.
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.
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.
Removed in 6135394
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.
Tested; the logic looks sound, thanks!
client/dashboard/data/site.ts
Outdated
{ fields: JOINED_SITE_FIELDS, options: JOINED_SITE_OPTIONS } | ||
); | ||
} catch ( error ) { | ||
if ( error instanceof Error && error.message.includes( '[-32710]' ) ) { |
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.
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.
Ohh I guess because there's more kind of errors that have the same parse_error
category
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.
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.
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.
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.
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.
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. |
Proposed Changes
More generally, this PR suggests an error handling strategy.
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
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