Files
probo/packages/helpers/src/error.ts
Sacha Al Himdani 21c4b7cd9d Add role management
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
2025-11-13 17:11:22 +01:00

30 lines
816 B
TypeScript

export interface GraphQLError {
message?: string;
extensions?: {
code?: string;
};
source?: {
errors?: Array<{ message: string; extensions?: { code?: 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('.') ? '' : '.'}`;
}