Improve graphql errors display in toast

This commit is contained in:
schroda
2025-07-27 03:42:29 +02:00
parent 20ca6d6cda
commit 56eabae9ae
3 changed files with 38 additions and 33 deletions

View File

@@ -7,6 +7,7 @@
*/
import { ApolloError } from '@apollo/client/errors';
import { ReactNode } from 'react';
export const jsonSaveParse = <T = any>(...args: Parameters<typeof JSON.parse>): T | null => {
try {
@@ -41,3 +42,30 @@ export const getValueFromObject = <T>(obj: Record<string, any>, key: string): T
export const coerceIn = (value: number, min: number, max: number): number => Math.max(Math.min(value, max), min);
export const noOp = () => {};
const GRAPHQL_EXCEPTION_MESSAGE_REGEX = /(.*Exception while fetching data \(.*\) : .*)\r\n\r\n(.*)/s;
export const extractGraphqlExceptionInfo = (
error: ReactNode | string,
): {
isGraphqlException: boolean;
graphqlError?: string;
graphqlStackTrace?: string;
} => {
if (typeof error !== 'string') {
return { isGraphqlException: false };
}
const regexMatch = error.match(GRAPHQL_EXCEPTION_MESSAGE_REGEX);
const isGraphqlException = !!regexMatch;
if (!isGraphqlException) {
return { isGraphqlException: false };
}
const [, message, stackTrace] = regexMatch;
return {
isGraphqlException: true,
graphqlError: message,
graphqlStackTrace: stackTrace,
};
};