Improve graphql errors display in toast
This commit is contained in:
@@ -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,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user