The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
[Metabot] Metabot error boundary (#67749)
Sloan Spargerd1a55ddff153
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/Metabot.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/Metabot.tsxindex 2a8070d6c50..7c73ba91ec8 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/Metabot.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/Metabot.tsx@@ -1,10 +1,14 @@-import { useEffect } from "react";+import { useEffect, useState } from "react";import { tinykeys } from "tinykeys";+import { t } from "ttag";+import MetabotFailure from "assets/img/metabot-failure.svg?component";import ErrorBoundary from "metabase/ErrorBoundary";import { useSelector } from "metabase/lib/redux";+import { Sidebar } from "metabase/nav/containers/MainNavbar/MainNavbar.styled";import type { SuggestionModel } from "metabase/rich_text_editing/tiptap/extensions/shared/types";import { getUser } from "metabase/selectors/user";+import { Box, Button, Flex, Text } from "metabase/ui";import { trackMetabotChatOpened } from "../analytics";import { useMetabotAgent } from "../hooks";@@ -12,6 +16,34 @@ import type { MetabotAgentId } from "../state";import { MetabotChat } from "./MetabotChat";+const MetabotErrorFallback = ({ onRetry }: { onRetry: () => void }) => {+ return (+ <Sidebar isOpen side="right" width="30rem">+ <Flex+ h="100%"+ gap="md"+ direction="column"+ align="center"+ justify="center"+ data-testid="metabot-error-fallback"+ >+ <Box component={MetabotFailure} w="6rem" />+ <Text c="text-light" maw="12rem" ta="center">+ {t`Something went wrong.`}+ </Text>+ <Button+ variant="subtle"+ size="compact-lg"+ onClick={onRetry}+ data-testid="metabot-error-retry"+ >+ {t`Try again`}+ </Button>+ </Flex>+ </Sidebar>+ );+};+// TODO: add test coverage for theseexport interface MetabotConfig {agentId?: MetabotAgentId;@@ -29,6 +61,9 @@ export interface MetabotProps {export const MetabotAuthenticated = ({ hide, config }: MetabotProps) => {const { visible, setVisible } = useMetabotAgent(config?.agentId ?? "omnibot");+ const [errorBoundaryKey, setErrorBoundaryKey] = useState(0);++ const handleRetry = () => setErrorBoundaryKey((prev) => prev + 1);useEffect(() => {return tinykeys(window, {@@ -55,8 +90,10 @@ export const MetabotAuthenticated = ({ hide, config }: MetabotProps) => {return null;}+ const ErrorFallback = () => <MetabotErrorFallback onRetry={handleRetry} />;+return (- <ErrorBoundary errorComponent={() => null}>+ <ErrorBoundary key={errorBoundaryKey} errorComponent={ErrorFallback}><MetabotChat config={config} /></ErrorBoundary>);diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/tests/error-boundary.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/tests/error-boundary.unit.spec.tsxnew file mode 100644index 00000000000..b55d25cd74d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/metabot/tests/error-boundary.unit.spec.tsx@@ -0,0 +1,112 @@+import { combineReducers } from "@reduxjs/toolkit";+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";+import { assocIn } from "icepick";++import { setupEnterprisePlugins } from "__support__/enterprise";+import { mockSettings } from "__support__/settings";+import { renderWithProviders, screen } from "__support__/ui";+import {+ createMockTokenFeatures,+ createMockUser,+} from "metabase-types/api/mocks";+import { createMockState } from "metabase-types/store/mocks";++import { Metabot } from "../components/Metabot";+import { FIXED_METABOT_IDS } from "../constants";+import { MetabotProvider } from "../context";+import { metabotReducer } from "../state";+import { getMetabotInitialState } from "../state/reducer-utils";++let mockShouldThrow = false;++jest.mock("../components/MetabotChat", () => {+ const metabotChatModule = jest.requireActual("../components/MetabotChat");+ return {+ ...metabotChatModule,+ MetabotChat: (props: any) => {+ if (mockShouldThrow) {+ throw new Error("Test error for ErrorBoundary");+ }+ return <metabotChatModule.MetabotChat {...props} />;+ },+ };+});++function setup() {+ const settings = mockSettings({+ "token-features": createMockTokenFeatures({+ metabot_v3: true,+ }),+ });++ setupEnterprisePlugins();++ const metabotState = assocIn(+ getMetabotInitialState(),+ ["conversations", "omnibot", "visible"],+ true,+ );++ fetchMock.get(+ `path:/api/ee/metabot-v3/metabot/${FIXED_METABOT_IDS.DEFAULT}/prompt-suggestions`,+ { prompts: [], offset: 0, limit: 3, total: 0 },+ );++ renderWithProviders(+ <MetabotProvider>+ <Metabot />+ </MetabotProvider>,+ {+ storeInitialState: createMockState({+ settings,+ currentUser: createMockUser(),+ plugins: {+ metabotPlugin: metabotState,+ },+ } as any),+ customReducers: {+ plugins: combineReducers({+ metabotPlugin: metabotReducer,+ }),+ },+ },+ );+}++describe("metabot error boundary", () => {+ beforeEach(() => {+ mockShouldThrow = false;+ });++ it("should show error fallback and recover when clicking try again", async () => {+ // prevent large amount of error content to get logged for our expected errors+ const consoleErrorSpy = jest+ .spyOn(console, "error")+ .mockImplementation(() => {});++ mockShouldThrow = true;++ setup();++ // Error fallback should be shown+ expect(+ await screen.findByTestId("metabot-error-fallback"),+ ).toBeInTheDocument();+ expect(screen.getByText("Something went wrong.")).toBeInTheDocument();++ // MetabotChat should not be visible+ expect(screen.queryByTestId("metabot-chat")).not.toBeInTheDocument();++ // Allow recovery on next render+ mockShouldThrow = false;++ await userEvent.click(screen.getByTestId("metabot-error-retry"));+ expect(await screen.findByTestId("metabot-chat")).toBeInTheDocument();+ expect(+ screen.queryByTestId("metabot-error-fallback"),+ ).not.toBeInTheDocument();++ consoleErrorSpy.mockRestore();+ });+});diff --git a/resources/frontend_client/app/assets/img/metabot-failure.svg b/resources/frontend_client/app/assets/img/metabot-failure.svgnew file mode 100644index 00000000000..4418a5a535f--- /dev/null+++ b/resources/frontend_client/app/assets/img/metabot-failure.svg@@ -0,0 +1,20 @@+<svg width="96" height="96" fill="none" xmlns="http://www.w3.org/2000/svg">+ <g clip-path="url(#a)">+ <circle cx="48" cy="48" r="48" fill="#4C5773" fill-opacity=".05"/>+ <path d="M24 35a4 4 0 0 1 4-4h40a4 4 0 0 1 4 4v26a4 4 0 0 1-4 4H28a4 4 0 0 1-4-4V35Z" fill="#fff"/>+ <path fill-rule="evenodd" clip-rule="evenodd" d="M23.375 35A4.625 4.625 0 0 1 28 30.375h40A4.625 4.625 0 0 1 72.625 35v26A4.625 4.625 0 0 1 68 65.625H28A4.625 4.625 0 0 1 23.375 61V35ZM28 31.625A3.375 3.375 0 0 0 24.625 35v26A3.375 3.375 0 0 0 28 64.375h40A3.375 3.375 0 0 0 71.375 61V35A3.375 3.375 0 0 0 68 31.625H28Z" fill="#949AAB"/>+ <path d="M29.5 38a1.5 1.5 0 0 1 1.5-1.5h34a1.5 1.5 0 0 1 1.5 1.5v17a1.5 1.5 0 0 1-1.5 1.5H31a1.5 1.5 0 0 1-1.5-1.5V38Z" fill="#fff"/>+ <path fill-rule="evenodd" clip-rule="evenodd" d="M28.875 38c0-1.174.951-2.125 2.125-2.125h34c1.174 0 2.125.951 2.125 2.125v17A2.125 2.125 0 0 1 65 57.125H31A2.125 2.125 0 0 1 28.875 55V38ZM31 37.125a.875.875 0 0 0-.875.875v17c0 .483.392.875.875.875h34a.875.875 0 0 0 .875-.875V38a.875.875 0 0 0-.875-.875H31ZM61.071 61.179a.464.464 0 1 0 0-.93.464.464 0 0 0 0 .93Zm0 1.25a1.714 1.714 0 1 0 0-3.429 1.714 1.714 0 0 0 0 3.429ZM65.571 61.179a.464.464 0 1 0 0-.93.464.464 0 0 0 0 .93Zm0 1.25a1.714 1.714 0 1 0 0-3.429 1.714 1.714 0 0 0 0 3.429Z" fill="#949AAB"/>+ <!-- Left X eye -->+ <path d="M39.5 42 L42.5 45 M42.5 42 L39.5 45" stroke="#949AAB" stroke-width="1.25" stroke-linecap="round"/>+ <!-- Right X eye -->+ <path d="M53.5 42 L56.5 45 M56.5 42 L53.5 45" stroke="#949AAB" stroke-width="1.25" stroke-linecap="round"/>+ <!-- Squiggly mouth -->+ <path d="M44 49 Q45.5 47.5 47 49 T50 49 T53 49" stroke="#949AAB" stroke-width="1.25" stroke-linecap="round" fill="none"/>+ </g>+ <defs>+ <clipPath id="a">+ <path fill="#fff" d="M0 0h96v96H0z"/>+ </clipPath>+ </defs>+</svg>