import { GraphQLErrorWrapperSchema } from "./graphql-error.schema"; export const extractGraphQLErrorMessage = (error: unknown): string => { const validationResult = GraphQLErrorWrapperSchema.safeParse(error); if (!validationResult.success) { return "An unexpected error occurred"; } const { response, message } = validationResult.data; if (response?.errors?.[0]?.message) { return response.errors[0].message; } if (message) { return message; } return "An unexpected error occurred"; }; export const extractGraphQLErrorCode = (error: unknown): string | null => { const validationResult = GraphQLErrorWrapperSchema.safeParse(error); if (!validationResult.success) { return null; } const firstError = validationResult.data.response?.errors?.[0]; return ( (firstError?.extensions as { code?: string } | undefined)?.code ?? null ); }; export const extractGraphQLErrorVariables = ( error: unknown, ): Record | null => { const validationResult = GraphQLErrorWrapperSchema.safeParse(error); if (!validationResult.success) { return null; } const firstError = validationResult.data.response?.errors?.[0]; return firstError?.extensions?.variables ?? null; };