Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions airflow-core/src/airflow/ui/src/components/ErrorAlert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import type { HTTPExceptionResponse, HTTPValidationError } from "openapi-gen/req

import { Alert } from "src/system-components";

import { getErrorDetail } from "src/utils/errorHandling";

export type ExpandedApiError = {
body: HTTPExceptionResponse | HTTPValidationError | undefined;
} & ApiError;
Expand All @@ -37,18 +39,7 @@ export const ErrorAlert = ({ error: err }: Props) => {
return undefined;
}

const details = error.body?.detail;
let detailMessage;

if (details !== undefined) {
if (typeof details === "string") {
detailMessage = details;
} else if (Array.isArray(details)) {
detailMessage = details.map((detail) => `${detail.loc.join(".")} ${detail.msg}`);
} else {
detailMessage = Object.keys(details).map((key) => `${key}: ${details[key] as string}`);
}
}
const detailMessage = getErrorDetail(error);

return (
<Alert data-testid="error-alert" status="error">
Expand Down
28 changes: 27 additions & 1 deletion airflow-core/src/airflow/ui/src/utils/errorHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
* under the License.
*/
import type { TFunction } from "i18next";
import type { HTTPExceptionResponse, HTTPValidationError } from "openapi-gen/requests/types.gen";

import { toaster } from "src/system-components";

/**
* Type guard to check if an error has a status property
*/
type ErrorWithStatus = {
body?: HTTPExceptionResponse | HTTPValidationError;
message?: string;
response?: {
status?: number;
Expand All @@ -44,6 +46,30 @@ export const getErrorStatus = (error: unknown): number | undefined => {
return errorObj.status ?? errorObj.response?.status;
};

export const getErrorDetail = (error: unknown): string | undefined => {
if (typeof error !== "object" || error === null) {
return undefined;
}

const detail = (error as ErrorWithStatus).body?.detail;

if (detail === undefined) {
return undefined;
}

if (typeof detail === "string") {
return detail;
}

if (Array.isArray(detail)) {
return detail.map((item) => `${item.loc.join(".")} ${item.msg}`).join("\n");
}

return Object.keys(detail)
.map((key) => `${key}: ${detail[key] as string}`)
.join("\n");
};

/**
* Safely extracts the error message from an error object
*/
Expand All @@ -54,7 +80,7 @@ const getErrorMessage = (error: unknown): string => {

const errorObj = error as ErrorWithStatus;

return errorObj.message ?? "An error occurred";
return getErrorDetail(error) ?? errorObj.message ?? "An error occurred";
};

/**
Expand Down
Loading