-
Notifications
You must be signed in to change notification settings - Fork 5
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
Surface query errors as notices #421
base: trunk
Are you sure you want to change the base?
Conversation
Test this PR in WordPress Playground. |
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 is great and sorely needed! I noticed that error responses in DataViews can obscure the error until the view is exited:
Screen.Recording.2025-03-14.at.10.57.03.AM.mov
Note: this request URL was modified to make it fail purposefully.
It'd be great if we had a way to also display errors in there, but either way this is an improvement as-is. Thank you!
The |
Ya, there isn't anything built into DataViews for this. How about adding a Notice as a child of our Screen.Recording.2025-03-14.at.8.53.42.PM.mov |
These are all great ideas. Thanks for looking into them! The notice works well because it's very prominent, but what if the user is currently editing five different blocks? The error doesn't specify which block is affected, and while we could make it smart and actionable, I think we'll be better off simplifying the UI and displaying the error in the block placeholder.
Could this work? |
Thanks so much for looking @jarekmorawski! I'll give it a shot. |
@chriszarate I've created a new I'm curious about your thoughts on implementing this. I would have thought to add it to Here is a demo of what the component looks like: Screen.Recording.2025-03-20.at.10.10.50.AM.movdiff --git a/src/blocks/remote-data-container/components/placeholders/PlaceholderError.tsx b/src/blocks/remote-data-container/components/placeholders/PlaceholderError.tsx
new file mode 100644
index 00000000..f3209a6b
--- /dev/null
+++ b/src/blocks/remote-data-container/components/placeholders/PlaceholderError.tsx
@@ -0,0 +1,48 @@
+import { Button, Placeholder, __experimentalHStack as HStack, Icon } from '@wordpress/components';
+import { useState } from '@wordpress/element';
+import { __, sprintf } from '@wordpress/i18n';
+import { caution } from '@wordpress/icons';
+
+import { RemoteDataFetchError } from '../../hooks/useRemoteData';
+
+interface PlaceholderErrorProps {
+ blockName: string;
+ error: Error;
+ onTryAgain: () => void;
+}
+
+export function PlaceholderError( { blockName, error, onTryAgain }: PlaceholderErrorProps ) {
+ const [ showErrorDetails, setShowErrorDetails ] = useState( false );
+
+ let message = error.message;
+ if ( error instanceof RemoteDataFetchError && error.cause instanceof Error ) {
+ message = `${ message }: ${ error.cause.message }`;
+ }
+
+ return (
+ <Placeholder
+ className="remote-data-container-error"
+ icon={ caution }
+ label={ sprintf( __( 'Cannot connect to remote data source: (%s)' ), blockName ) }
+ >
+ <Icon className="remote-data-container-error__icon-overlay" icon={ caution } />
+ <HStack justify="flex-start">
+ <Button variant="secondary" onClick={ onTryAgain }>
+ { __( 'Try again' ) }
+ </Button>
+ <Button variant="tertiary" onClick={ () => setShowErrorDetails( ! showErrorDetails ) }>
+ { sprintf( __( '%s error details' ), showErrorDetails ? 'Hide' : 'Show' ) }
+ </Button>
+ </HStack>
+ { showErrorDetails && (
+ <code
+ className={ `remote-data-container-error__content is-${
+ showErrorDetails ? 'open' : 'closed'
+ }` }
+ >
+ { message }
+ </code>
+ ) }
+ </Placeholder>
+ );
+}
diff --git a/src/blocks/remote-data-container/editor.scss b/src/blocks/remote-data-container/editor.scss
index 238fc2d4..90185279 100644
--- a/src/blocks/remote-data-container/editor.scss
+++ b/src/blocks/remote-data-container/editor.scss
@@ -4,6 +4,69 @@
@import url(@wordpress/dataviews/build-style/style.css);
+// selected error placeholder
+.rdb-container.is-selected .components-placeholder.remote-data-container-error {
+
+ .components-placeholder__label > svg {
+ fill: var(--Gutenberg-Alert-Red, #cc1818);
+ }
+
+ .remote-data-container-error__content {
+ width: auto;
+ background: var(--Gutenberg-Gray-100, #f0f0f0);
+ color: var(--Gutenberg-Gray-900, #1e1e1e);
+ line-height: 1.5;
+ padding: 12px 16px;
+ border-radius: 2px;
+ }
+
+ // hide overlay icon
+ .remote-data-container-error__icon-overlay {
+ display: none;
+ }
+}
+
+// unselected error placeholder
+.rdb-container:not(.is-selected) .components-placeholder.remote-data-container-error {
+ background: var(--Gutenberg-Gray-100, #f0f0f0);
+ box-shadow: inset 0 0 1px 1px var(--Gutenberg-Alert-Red, #cc1818);
+
+ // hide content when block is not selected
+ *:not(.remote-data-container-error__icon-overlay):not(.remote-data-container-error__icon-overlay *) {
+ visibility: hidden;
+ }
+
+ // hide open error details when block is not selected for consistent sizing
+ .remote-data-container-error__content.is-open {
+ display: none;
+ }
+
+ // add strikethrough across placeholder
+ &::before {
+ content: "";
+ position: absolute;
+ top: 0;
+ left: 0;
+ width: 100%;
+ height: 100%;
+ background: var(--Gutenberg-Gray-200, #e0e0e0);
+ clip-path: polygon(1% 1%, 2% 1%, 99% 99%, 98% 99%);
+ }
+
+ // show overlay icon when block is not selected
+ .remote-data-container-error__icon-overlay {
+ visibility: visible;
+ width: 48px;
+ height: 48px;
+ background: var(--Gutenberg-Gray-100, #f0f0f0);
+ fill: var(--Gutenberg-Gray-200, #e0e0e0);
+ position: absolute;
+ top: 50%;
+ left: 50%;
+ transform: translate(-50%, -50%);
+ }
+}
+
.remote-data-blocks-loading-overlay {
align-items: center;
background-color: rgba(255, 255, 255, 0.25);
|
@brookewp Push it up with thanks! I think |
Signed-off-by: brookewp <[email protected]>
Done!
When destructuring remote-data-blocks/src/blocks/remote-data-container/edit.tsx Lines 42 to 47 in 500df71
|
|
I have a test branch here: I'm looking into it, but I'm sure you'll be quicker than me. |
Btw, if it's helpful, Edit: I had added comments in my test branch to demonstrate this, but not sure if they stand out so wanted to mention: remote-data-blocks/src/blocks/remote-data-container/edit.tsx Lines 47 to 50 in 7486060
So I'm not sure if we'd want modify edit to look at the other keys, or adjust how the error works in |
Ah, I see. So we have the "render" usage of We could use context. What about throwing and using an error boundary? Are we able to recover / try again? |
I can give that a try and report back |
Screen.Recording.2025-03-13.at.16.50.45.mov
The error duplication should be separately fixed by #416