diff --git a/webapp/packages/core-sdk/src/CustomGraphQLClient.ts b/webapp/packages/core-sdk/src/CustomGraphQLClient.ts index cbdbfb616db..47063f6b2d1 100644 --- a/webapp/packages/core-sdk/src/CustomGraphQLClient.ts +++ b/webapp/packages/core-sdk/src/CustomGraphQLClient.ts @@ -6,20 +6,20 @@ * you may not use this file except in compliance with the License. */ -import { GraphQLClient } from 'graphql-request'; -import { ClientError } from 'graphql-request'; +import { GraphQLClient, ClientError } from 'graphql-request'; import type { Variables } from 'graphql-request/dist/types'; import type * as Dom from 'graphql-request/dist/types.dom'; import { GQLError } from './GQLError'; import type { IResponseInterceptor } from './IResponseInterceptor'; +import { PlainGQLError } from './PlainGQLError'; export class CustomGraphQLClient extends GraphQLClient { get blockReason(): Error | string | null { return this.requestsBlockedReason; } - private interceptors: IResponseInterceptor[] = []; + private readonly interceptors: IResponseInterceptor[] = []; private isRequestsBlocked = false; private requestsBlockedReason: Error | string | null = null; @@ -27,7 +27,7 @@ export class CustomGraphQLClient extends GraphQLClient { this.interceptors.push(interceptor); } - request( + request( query: string, variables?: V, requestHeaders?: Dom.RequestInit['headers'] @@ -67,8 +67,13 @@ export class CustomGraphQLClient extends GraphQLClient { return response.data as T; } catch (error) { if (isClientError(error)) { - throw new GQLError(error); + if (isObjectError(error)) { + throw new GQLError(error); + } else { + throw new PlainGQLError(error); + } } + throw error; } } @@ -78,3 +83,7 @@ function isClientError(obj: any): obj is ClientError { // in es5 build `instanceof ClientError` always false, so we try to determine by checking response property return obj instanceof ClientError || obj.response; } + +function isObjectError(obj: ClientError) { + return obj.response.data !== undefined && !!obj.response.errors; +} diff --git a/webapp/packages/core-sdk/src/PlainGQLError.ts b/webapp/packages/core-sdk/src/PlainGQLError.ts new file mode 100644 index 00000000000..0031e185b2b --- /dev/null +++ b/webapp/packages/core-sdk/src/PlainGQLError.ts @@ -0,0 +1,35 @@ +/* + * CloudBeaver - Cloud Database Manager + * Copyright (C) 2020-2022 DBeaver Corp and others + * + * Licensed under the Apache License, Version 2.0. + * you may not use this file except in compliance with the License. + */ + +import type { ClientError } from 'graphql-request'; +import type { GraphQLRequestContext, GraphQLResponse } from 'graphql-request/dist/types'; + +import { getTextBetween } from '@cloudbeaver/core-utils'; + +import { DetailsError } from './DetailsError'; + +export class PlainGQLError extends DetailsError { + response: GraphQLResponse; + request: GraphQLRequestContext; + constructor(clientError: ClientError) { + let message = clientError.message; + + if (typeof clientError.response.error === 'string') { + message = getTextBetween(clientError.response.error, '', ''); + } + + super(message); + this.name = 'GQL Error'; + this.response = clientError.response; + this.request = clientError.request; + } + + hasDetails(): boolean { + return false; + } +} \ No newline at end of file diff --git a/webapp/packages/core-utils/src/getTextBetween.ts b/webapp/packages/core-utils/src/getTextBetween.ts new file mode 100644 index 00000000000..1dec86be1e9 --- /dev/null +++ b/webapp/packages/core-utils/src/getTextBetween.ts @@ -0,0 +1,15 @@ +/* + * CloudBeaver - Cloud Database Manager + * Copyright (C) 2020-2022 DBeaver Corp and others + * + * Licensed under the Apache License, Version 2.0. + * you may not use this file except in compliance with the License. + */ + +export function getTextBetween(target: string, left: string, right: string) { + if (!target.includes(left) || !target.includes(right)) { + return target; + } + + return target.slice(target.indexOf(left) + left.length, target.lastIndexOf(right)); +} diff --git a/webapp/packages/core-utils/src/index.ts b/webapp/packages/core-utils/src/index.ts index 6e080049207..dc77dcf8ee1 100644 --- a/webapp/packages/core-utils/src/index.ts +++ b/webapp/packages/core-utils/src/index.ts @@ -30,3 +30,4 @@ export * from './isMapsEqual'; export * from './openCenteredPopup'; export * from './download'; export * from './getTextFileReadingProcess'; +export * from './getTextBetween';