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('.') ? '' : '.'}`;
|
||||
}
|
||||
Reference in New Issue
Block a user