Skip to content

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

Show errors from the body of http 500 responses to users (#74693)

Aleksandr Lesnenko8444d69eb9b7
diff --git a/enterprise/frontend/src/custom-viz/src/types/data.ts b/enterprise/frontend/src/custom-viz/src/types/data.ts
index 39a4cb7341a..8dd0ded1daf 100644
--- a/enterprise/frontend/src/custom-viz/src/types/data.ts
+++ b/enterprise/frontend/src/custom-viz/src/types/data.ts
@@ -20,7 +20,7 @@ export type DatasetError =
| string
| {
status: number; // HTTP status code
- data?: string;
+ data?: unknown;
};
export type SingleSeries = {
diff --git a/frontend/src/metabase-types/api/dataset.ts b/frontend/src/metabase-types/api/dataset.ts
index 081b7796ee2..373f3291c65 100644
--- a/frontend/src/metabase-types/api/dataset.ts
+++ b/frontend/src/metabase-types/api/dataset.ts
@@ -154,7 +154,7 @@ export type DatasetError =
| string
| {
status: number; // HTTP status code
- data?: string;
+ data?: unknown;
};
export type DatasetErrorType =
diff --git a/frontend/src/metabase/documents/components/editor-extensions/CardEmbed/modals/NativeQueryModal.tsx b/frontend/src/metabase/documents/components/editor-extensions/CardEmbed/modals/NativeQueryModal.tsx
index 163ade7bddb..b58ea40a14d 100644
--- a/frontend/src/metabase/documents/components/editor-extensions/CardEmbed/modals/NativeQueryModal.tsx
+++ b/frontend/src/metabase/documents/components/editor-extensions/CardEmbed/modals/NativeQueryModal.tsx
@@ -4,6 +4,7 @@ import { c, t } from "ttag";
import EmptyCodeResult from "assets/img/empty-states/code.svg";
import { datasetApi } from "metabase/api/dataset";
+import { getErrorMessage as getResponseErrorMessage } from "metabase/api/utils";
import { ErrorMessage } from "metabase/common/components/ErrorMessage";
import { getMetadata } from "metabase/metadata-store";
import { DataReference } from "metabase/querying/components/DataReference/DataReference";
@@ -59,9 +60,10 @@ const getErrorMessage = (
queryError: unknown,
): string => {
if (failedDataset?.error) {
- return typeof failedDataset.error === "string"
- ? failedDataset.error
- : failedDataset.error?.data || t`Query execution failed`;
+ return getResponseErrorMessage(
+ failedDataset.error,
+ t`Query execution failed`,
+ );
}
if (typeof queryError === "object" && queryError && "message" in queryError) {
return String(queryError.message);
diff --git a/frontend/src/metabase/metadata/components/PreviewSection/utils.ts b/frontend/src/metabase/metadata/components/PreviewSection/utils.ts
index f2fbc1a505a..56343bd8f2c 100644
--- a/frontend/src/metabase/metadata/components/PreviewSection/utils.ts
+++ b/frontend/src/metabase/metadata/components/PreviewSection/utils.ts
@@ -1,5 +1,6 @@
import { t } from "ttag";
+import { getErrorMessage } from "metabase/api/utils";
import type { Dataset, Field } from "metabase-types/api";
export { is403Error } from "metabase/utils/errors";
@@ -13,8 +14,6 @@ export function getPreviewTypeData() {
}
export function getDataErrorMessage(data: Dataset): string {
- const error = typeof data.error === "string" ? data.error : data.error?.data;
-
if (data.error_type === "invalid-query") {
return t`Something went wrong fetching the data for this field. This could mean something is wrong with the field settings, like a cast that is not supported for the underlying data type. Please check your settings and try again.`;
}
@@ -23,7 +22,7 @@ export function getDataErrorMessage(data: Dataset): string {
return t`You do not have permission to preview this field's data.`;
}
- return error ?? t`Something went wrong`;
+ return getErrorMessage(data.error);
}
export function isFieldHidden(field: Field) {
diff --git a/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/VisualizationError.tsx b/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/VisualizationError.tsx
index 0682dff7b2e..051b6ac4dc2 100644
--- a/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/VisualizationError.tsx
+++ b/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/VisualizationError.tsx
@@ -23,7 +23,7 @@ import { VISUALIZATION_SLOW_TIMEOUT } from "../../../constants";
import VisErrorS from "./VisualizationError.module.css";
import { AdminEmail } from "./components";
-import { adjustPositions, stripRemarks } from "./utils";
+import { adjustPositions, getDatasetErrorMessage, stripRemarks } from "./utils";
interface VisualizationErrorProps {
className?: string;
@@ -63,36 +63,38 @@ export function VisualizationError({
);
}
+ const errorMessage = getDatasetErrorMessage(error);
// Treat transport-level failures (server dropped connection, offline, etc.)
// the same as an HTTP error with a status — the user just needs to know the
// server isn't reachable, not see a stack trace.
- if (
+ const isTransportError =
isNetworkError(error) ||
- (typeof error === "object" && error.status != null)
- ) {
- // Assume if the request took more than 15 seconds it was due to a timeout
- // Some platforms like Heroku return a 503 for numerous types of errors so we can't use the status code to distinguish between timeouts and other failures.
- if (duration > VISUALIZATION_SLOW_TIMEOUT) {
- return (
- <ErrorMessage
- className={className}
- type="timeout"
- title={t`Your question took too long`}
- message={t`We didn't get an answer back from your database in time, so we had to stop. You can try again in a minute, or if the problem persists, you can email an admin to let them know.`}
- action={<AdminEmail />}
- />
- );
- } else {
- return (
- <ErrorMessage
- className={className}
- type="serverError"
- title={t`We're experiencing server issues`}
- message={t`Try refreshing the page after waiting a minute or two. If the problem persists we'd recommend you contact an admin.`}
- action={<AdminEmail />}
- />
- );
- }
+ (typeof error === "object" && error.status != null);
+
+ // Assume if the request took more than 15 seconds it was due to a timeout
+ // Some platforms like Heroku return a 503 for numerous types of errors so we can't use the status code to distinguish between timeouts and other failures.
+ if (isTransportError && duration > VISUALIZATION_SLOW_TIMEOUT) {
+ return (
+ <ErrorMessage
+ className={className}
+ type="timeout"
+ title={t`Your question took too long`}
+ message={t`We didn't get an answer back from your database in time, so we had to stop. You can try again in a minute, or if the problem persists, you can email an admin to let them know.`}
+ action={<AdminEmail />}
+ />
+ );
+ }
+
+ if (isTransportError && !errorMessage) {
+ return (
+ <ErrorMessage
+ className={className}
+ type="serverError"
+ title={t`We're experiencing server issues`}
+ message={t`Try refreshing the page after waiting a minute or two. If the problem persists we'd recommend you contact an admin.`}
+ action={<AdminEmail />}
+ />
+ );
}
if (errorType === "missing-required-permissions") {
@@ -135,7 +137,7 @@ export function VisualizationError({
if (isNative) {
// always show errors for native queries
- let processedError = typeof error === "string" ? error : error.data;
+ let processedError = errorMessage;
const origSql = getIn(via, [(via || "").length - 1, "ex-data", "sql"]);
if (typeof origSql === "string" && processedError) {
processedError = adjustPositions(processedError, origSql);
@@ -195,7 +197,7 @@ export function VisualizationError({
<p
className={QueryBuilderS.QueryErrorMessageText}
>{t`Most of the time this is caused by an invalid selection or bad input value. Double check your inputs and retry your query.`}</p>
- <ErrorDetails className={CS.pt2} details={error} />
+ <ErrorDetails className={CS.pt2} details={errorMessage} />
</div>
</div>
);
diff --git a/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/tests/common.unit.spec.ts b/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/tests/common.unit.spec.ts
index a2ed87a17a0..489ef055659 100644
--- a/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/tests/common.unit.spec.ts
+++ b/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/tests/common.unit.spec.ts
@@ -1,5 +1,6 @@
import { screen } from "__support__/ui";
import { StreamInterruptedError } from "metabase/api/client";
+import type { DatasetError } from "metabase-types/api";
import { createMockCard, createMockDatabase } from "metabase-types/api/mocks";
import { setup } from "./setup";
@@ -61,6 +62,56 @@ describe("VisualizationError (OSS)", () => {
});
});
+ describe("server errors", () => {
+ // A failed query arrives as { status, data }, data being the response body.
+ const createServerError = (data: unknown): DatasetError => ({
+ status: 500,
+ data,
+ });
+
+ const database = createMockDatabase({ engine: "postgres" });
+ const card = createMockCard({
+ dataset_query: {
+ database: database.id,
+ type: "native",
+ native: { query: "SELECT * FROM ORDERS" },
+ },
+ });
+
+ it("shows the message from a 500 whose payload carries one the same way as for 4xx", () => {
+ const message =
+ "Too many query parameters: 13444 exceeds limit of 10000.";
+ setup({
+ database,
+ card,
+ error: createServerError({ status: "failed", error: message }),
+ });
+
+ expect(
+ screen.getByText("An error occurred in your query"),
+ ).toBeInTheDocument();
+ expect(screen.getByText(message)).toBeInTheDocument();
+ expect(
+ screen.queryByText("We're experiencing server issues"),
+ ).not.toBeInTheDocument();
+ });
+
+ it("falls back to the generic server-issues card when a 500 carries no usable message", () => {
+ setup({
+ database,
+ card,
+ error: createServerError("<html>Internal Server Error</html>"),
+ });
+
+ expect(
+ screen.getByText("We're experiencing server issues"),
+ ).toBeInTheDocument();
+ expect(
+ screen.queryByText("An error occurred in your query"),
+ ).not.toBeInTheDocument();
+ });
+ });
+
describe("NoSQL databases", () => {
const database = createMockDatabase({
engine: "mongo",
diff --git a/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/tests/setup.tsx b/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/tests/setup.tsx
index 698782baced..72c6d793a49 100644
--- a/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/tests/setup.tsx
+++ b/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/tests/setup.tsx
@@ -10,6 +10,7 @@ import type {
Card,
Database,
DatasetError,
+ DatasetErrorType,
TokenFeatures,
} from "metabase-types/api";
import {
@@ -29,6 +30,8 @@ export interface SetupOpts {
showMetabaseLinks?: boolean;
tokenFeatures?: Partial<TokenFeatures>;
enterprisePlugins?: Parameters<typeof setupEnterpriseOnlyPlugin>[0][];
+ errorType?: DatasetErrorType;
+ duration?: number;
}
export const setup = ({
@@ -38,6 +41,8 @@ export const setup = ({
showMetabaseLinks = true,
tokenFeatures = {},
enterprisePlugins = [],
+ errorType,
+ duration = 0,
}: SetupOpts) => {
const state = createMockState({
entities: createMockEntitiesState({
@@ -65,9 +70,10 @@ export const setup = ({
renderWithProviders(
<VisualizationError
question={question}
- duration={0}
+ duration={duration}
// Unjustified type cast. FIXME
error={error as DatasetError}
+ errorType={errorType}
via={[]}
/>,
{ storeInitialState: state },
diff --git a/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/utils.ts b/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/utils.ts
index 14e2778c31e..81dc9342f29 100644
--- a/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/utils.ts
+++ b/frontend/src/metabase/querying/components/QueryVisualization/VisualizationError/utils.ts
@@ -1,3 +1,18 @@
+import { getErrorMessage } from "metabase/api/utils";
+import type { DatasetError } from "metabase-types/api";
+
+// Error message for a failed query. 4xx comes as a string, 5xx as { status, data }.
+// Returns "" when there is no message in the payload. (#71558)
+export function getDatasetErrorMessage(error: DatasetError): string {
+ if (typeof error === "string") {
+ return error;
+ }
+ const { data } = error;
+ return data != null && typeof data === "object"
+ ? getErrorMessage(data, "")
+ : "";
+}
+
export function adjustPositions(error: string, origSql: string): string {
/* Positions in error messages are borked coming in for Postgres errors.
* Previously, you would see "blahblahblah bombed out, Position: 119" in a 10-character invalid query.