Skip to content

Commit dd82ef2

Browse files
authored
Merge pull request dbeaver#624 from dbeaver/fix/html-error
fix(core-sdk): CB-1523 handle html error text
2 parents 19a5fc6 + 796eb56 commit dd82ef2

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

webapp/packages/core-sdk/src/CustomGraphQLClient.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@
66
* you may not use this file except in compliance with the License.
77
*/
88

9-
import { GraphQLClient } from 'graphql-request';
10-
import { ClientError } from 'graphql-request';
9+
import { GraphQLClient, ClientError } from 'graphql-request';
1110
import type { Variables } from 'graphql-request/dist/types';
1211
import type * as Dom from 'graphql-request/dist/types.dom';
1312

1413
import { GQLError } from './GQLError';
1514
import type { IResponseInterceptor } from './IResponseInterceptor';
15+
import { PlainGQLError } from './PlainGQLError';
1616

1717
export class CustomGraphQLClient extends GraphQLClient {
1818
get blockReason(): Error | string | null {
1919
return this.requestsBlockedReason;
2020
}
2121

22-
private interceptors: IResponseInterceptor[] = [];
22+
private readonly interceptors: IResponseInterceptor[] = [];
2323
private isRequestsBlocked = false;
2424
private requestsBlockedReason: Error | string | null = null;
2525

2626
registerInterceptor(interceptor: IResponseInterceptor): void {
2727
this.interceptors.push(interceptor);
2828
}
2929

30-
request<T extends any, V = Variables>(
30+
request<T, V = Variables>(
3131
query: string,
3232
variables?: V,
3333
requestHeaders?: Dom.RequestInit['headers']
@@ -67,8 +67,13 @@ export class CustomGraphQLClient extends GraphQLClient {
6767
return response.data as T;
6868
} catch (error) {
6969
if (isClientError(error)) {
70-
throw new GQLError(error);
70+
if (isObjectError(error)) {
71+
throw new GQLError(error);
72+
} else {
73+
throw new PlainGQLError(error);
74+
}
7175
}
76+
7277
throw error;
7378
}
7479
}
@@ -78,3 +83,7 @@ function isClientError(obj: any): obj is ClientError {
7883
// in es5 build `instanceof ClientError` always false, so we try to determine by checking response property
7984
return obj instanceof ClientError || obj.response;
8085
}
86+
87+
function isObjectError(obj: ClientError) {
88+
return obj.response.data !== undefined && !!obj.response.errors;
89+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* CloudBeaver - Cloud Database Manager
3+
* Copyright (C) 2020-2022 DBeaver Corp and others
4+
*
5+
* Licensed under the Apache License, Version 2.0.
6+
* you may not use this file except in compliance with the License.
7+
*/
8+
9+
import type { ClientError } from 'graphql-request';
10+
import type { GraphQLRequestContext, GraphQLResponse } from 'graphql-request/dist/types';
11+
12+
import { getTextBetween } from '@cloudbeaver/core-utils';
13+
14+
import { DetailsError } from './DetailsError';
15+
16+
export class PlainGQLError extends DetailsError {
17+
response: GraphQLResponse;
18+
request: GraphQLRequestContext;
19+
constructor(clientError: ClientError) {
20+
let message = clientError.message;
21+
22+
if (typeof clientError.response.error === 'string') {
23+
message = getTextBetween(clientError.response.error, '<title>', '</title>');
24+
}
25+
26+
super(message);
27+
this.name = 'GQL Error';
28+
this.response = clientError.response;
29+
this.request = clientError.request;
30+
}
31+
32+
hasDetails(): boolean {
33+
return false;
34+
}
35+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* CloudBeaver - Cloud Database Manager
3+
* Copyright (C) 2020-2022 DBeaver Corp and others
4+
*
5+
* Licensed under the Apache License, Version 2.0.
6+
* you may not use this file except in compliance with the License.
7+
*/
8+
9+
export function getTextBetween(target: string, left: string, right: string) {
10+
if (!target.includes(left) || !target.includes(right)) {
11+
return target;
12+
}
13+
14+
return target.slice(target.indexOf(left) + left.length, target.lastIndexOf(right));
15+
}

webapp/packages/core-utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ export * from './isMapsEqual';
3030
export * from './openCenteredPopup';
3131
export * from './download';
3232
export * from './getTextFileReadingProcess';
33+
export * from './getTextBetween';

0 commit comments

Comments
 (0)