Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Make question moving errors bubble correctly (#63409)

Ryan Laurie0d1522742ed4
diff --git a/e2e/test/scenarios/question/question-management.cy.spec.js b/e2e/test/scenarios/question/question-management.cy.spec.js
index f43614e1e6b..51cb8c16819 100644
--- a/e2e/test/scenarios/question/question-management.cy.spec.js
+++ b/e2e/test/scenarios/question/question-management.cy.spec.js
@@ -437,6 +437,50 @@ describe(
},
);
+describe("question moving", () => {
+ beforeEach(() => {
+ H.restore();
+ cy.signInAsAdmin();
+ cy.intercept("PUT", `/api/card/${ORDERS_QUESTION_ID}`).as("updateQuestion");
+ H.visitQuestion(ORDERS_QUESTION_ID);
+ });
+
+ it("should move a question between collections", () => {
+ H.appBar().findByText("Our analytics").should("be.visible");
+ cy.findByTestId("qb-header-action-panel")
+ .icon("ellipsis")
+ .closest("button")
+ .click();
+ H.popover().findByTestId("move-button").click();
+ H.modal().findByText("Second collection").click();
+ H.modal().button("Move").click();
+ cy.wait("@updateQuestion").its("response.statusCode").should("eq", 200);
+ cy.findAllByRole("status").contains("Question moved to Second collection");
+ H.appBar().findByText("Second collection").should("be.visible");
+ H.modal().should("not.exist");
+ });
+
+ it("should show an error when moving a question fails", () => {
+ cy.intercept("PUT", `/api/card/${ORDERS_QUESTION_ID}`, {
+ statusCode: 400,
+ body: { message: "Sorry buddy, only cool kids in this collection" },
+ }).as("updateQuestion");
+
+ H.appBar().findByText("Our analytics").should("be.visible");
+ cy.findByTestId("qb-header-action-panel")
+ .icon("ellipsis")
+ .closest("button")
+ .click();
+ H.popover().findByTestId("move-button").click();
+ H.modal().findByText("Second collection").click();
+ H.modal().button("Move").click();
+ cy.wait("@updateQuestion");
+ H.modal()
+ .findByText("Sorry buddy, only cool kids in this collection")
+ .should("be.visible");
+ });
+});
+
H.describeWithSnowplow("send snowplow question events", () => {
beforeEach(() => {
H.restore();
diff --git a/frontend/src/metabase/common/components/EntityPicker/components/EntityPickerModal/ButtonBar.tsx b/frontend/src/metabase/common/components/EntityPicker/components/EntityPickerModal/ButtonBar.tsx
index 1e4bb05819d..009ba13cf34 100644
--- a/frontend/src/metabase/common/components/EntityPicker/components/EntityPickerModal/ButtonBar.tsx
+++ b/frontend/src/metabase/common/components/EntityPicker/components/EntityPickerModal/ButtonBar.tsx
@@ -1,6 +1,7 @@
import { useEffect, useState } from "react";
import { t } from "ttag";
+import { getErrorMessage } from "metabase/api/utils";
import { Button, Flex, Text } from "metabase/ui";
export const ButtonBar = ({
@@ -63,7 +64,7 @@ export const ButtonBar = ({
await onConfirm();
} catch (e: any) {
console.error(e);
- setError(e?.data?.message ?? t`An error occurred`);
+ setError(getErrorMessage(e));
}
}}
disabled={!canConfirm}
diff --git a/frontend/src/metabase/lib/errors/messages.ts b/frontend/src/metabase/lib/errors/messages.ts
index d66ccb13841..82f9c5a570a 100644
--- a/frontend/src/metabase/lib/errors/messages.ts
+++ b/frontend/src/metabase/lib/errors/messages.ts
@@ -1,5 +1,8 @@
import type { GenericErrorResponse } from "./types";
+/**
+ * @deprecated Use `getErrorMessage` from `metabase/api/utils` instead.
+ */
export function getResponseErrorMessage(error: unknown): string | undefined {
const response = error as GenericErrorResponse | undefined;
@@ -26,7 +29,3 @@ export function getResponseErrorMessage(error: unknown): string | undefined {
return undefined;
}
-
-export function isResourceNotFoundError(error: any) {
- return error?.status === 404;
-}
diff --git a/frontend/src/metabase/query_builder/components/MoveQuestionModal/MoveQuestionModal.tsx b/frontend/src/metabase/query_builder/components/MoveQuestionModal/MoveQuestionModal.tsx
index 69e537d4d7c..19767548a16 100644
--- a/frontend/src/metabase/query_builder/components/MoveQuestionModal/MoveQuestionModal.tsx
+++ b/frontend/src/metabase/query_builder/components/MoveQuestionModal/MoveQuestionModal.tsx
@@ -4,6 +4,7 @@ import { c, t } from "ttag";
import _ from "underscore";
import { getDashboard, useUpdateCardMutation } from "metabase/api";
+import { getErrorMessage } from "metabase/api/utils";
import { QuestionMoveConfirmModal } from "metabase/collections/components/CollectionBulkActions/QuestionMoveConfirmModal";
import type { MoveDestination } from "metabase/collections/types";
import { canonicalCollectionId } from "metabase/collections/utils";
@@ -12,7 +13,6 @@ import { MoveModal } from "metabase/common/components/MoveModal";
import type { CollectionPickerItem } from "metabase/common/components/Pickers/CollectionPicker";
import Dashboards from "metabase/entities/dashboards";
import { INJECT_RTK_QUERY_QUESTION_VALUE } from "metabase/entities/questions";
-import { getResponseErrorMessage } from "metabase/lib/errors";
import { useDispatch } from "metabase/lib/redux";
import * as Urls from "metabase/lib/urls";
import { API_UPDATE_QUESTION } from "metabase/query_builder/actions";
@@ -51,7 +51,7 @@ export const MoveQuestionModal = ({
const [errorMessage, setErrorMessage] = useState<string>();
- const handleMove = async (
+ const handleMove = (
destination: MoveDestination,
deleteOldDashcards?: boolean | undefined,
) => {
@@ -63,7 +63,7 @@ export const MoveQuestionModal = ({
collection_id: canonicalCollectionId(destination.id),
};
- await updateQuestion({
+ return updateQuestion({
id: question.id(),
delete_old_dashcards: deleteOldDashcards,
...update,
@@ -116,13 +116,18 @@ export const MoveQuestionModal = ({
onClose();
})
.catch((e) => {
- setErrorMessage(getResponseErrorMessage(e));
+ setErrorMessage(getErrorMessage(e));
+ if (destination.model !== "dashboard") {
+ // we want this error to bubble up to the modal if we're not
+ // showing the dashboard confirm modal
+ throw new Error(getErrorMessage(e));
+ }
});
};
- const handleMoveConfirm = () => {
+ const handleMoveConfirm = async () => {
if (confirmMoveState?.destination) {
- handleMove(confirmMoveState?.destination, true);
+ await handleMove(confirmMoveState?.destination, true);
}
};
@@ -142,7 +147,7 @@ export const MoveQuestionModal = ({
destination,
});
} else {
- handleMove(destination);
+ await handleMove(destination);
}
};
@@ -243,7 +248,7 @@ export const MoveQuestionModal = ({
]}
onConfirm={handleMoveConfirm}
onClose={onClose}
- destination={confirmMoveState.destination}
+ destination={confirmMoveState?.destination}
errorMessage={errorMessage}
/>
);