26
packages/helpers/src/error.ts
Normal file
26
packages/helpers/src/error.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
export interface GraphQLError {
|
||||
message?: string;
|
||||
source?: {
|
||||
errors?: Array<{ message: string }>;
|
||||
};
|
||||
}
|
||||
|
||||
export function formatError(title: string, error: GraphQLError | GraphQLError[]): string {
|
||||
const messages: string[] = [];
|
||||
|
||||
if (Array.isArray(error)) {
|
||||
messages.push(...error.map((e) => e.message).filter(Boolean) as string[]);
|
||||
} else if (error.source?.errors && Array.isArray(error.source.errors)) {
|
||||
messages.push(...error.source.errors.map((e) => e.message).filter(Boolean));
|
||||
} else if (error.message) {
|
||||
messages.push(error.message);
|
||||
}
|
||||
|
||||
if (messages.length === 0) {
|
||||
return title;
|
||||
}
|
||||
|
||||
const errorList = messages.join(", ");
|
||||
|
||||
return `${title}: ${errorList}${errorList.endsWith('.') ? '' : '.'}`;
|
||||
}
|
||||
@@ -59,3 +59,4 @@ export { promisifyMutation } from "./relay";
|
||||
export { fileType, fileSize } from "./file";
|
||||
export { formatDatetime, formatDate } from "./date";
|
||||
export { getLogoUrl, getTrustCenterUrl } from "./trustCenter";
|
||||
export { formatError, type GraphQLError } from "./error";
|
||||
|
||||
@@ -12,6 +12,6 @@ type Story = StoryObj<typeof ErrorLayout>;
|
||||
export const Default: Story = {
|
||||
args: {
|
||||
title: "Something went wrong",
|
||||
description: "An unexpected error occurred. Please try again later.",
|
||||
description: "An unexpected error occurred",
|
||||
},
|
||||
};
|
||||
|
||||
@@ -82,10 +82,8 @@ export function ConfirmDialog() {
|
||||
const handleConfirm = () => {
|
||||
setLoading(true);
|
||||
onConfirm()
|
||||
.then(() => {
|
||||
close();
|
||||
})
|
||||
.finally(() => {
|
||||
close();
|
||||
setLoading(false);
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user