From 1dcfdcf1ee1447a88444efd9b264eea9d7195ca5 Mon Sep 17 00:00:00 2001 From: Wroud Date: Mon, 11 May 2020 15:31:51 +0300 Subject: [PATCH] feat(core): GQLErrorCatcher helper class --- .../packages/core/src/root/ServerService.ts | 3 +- .../packages/core/src/sdk/GQLErrorCatcher.ts | 34 +++++++++++++++++++ webapp/packages/core/src/sdk/index.ts | 1 + .../ConnectionFormDialog.tsx | 7 ++-- .../ConnectionFormDialogController.ts | 27 ++++----------- .../CustomConnectionDialog.tsx | 4 +-- 6 files changed, 48 insertions(+), 28 deletions(-) create mode 100644 webapp/packages/core/src/sdk/GQLErrorCatcher.ts diff --git a/webapp/packages/core/src/root/ServerService.ts b/webapp/packages/core/src/root/ServerService.ts index 6dde91fc2b..e87d172b6a 100644 --- a/webapp/packages/core/src/root/ServerService.ts +++ b/webapp/packages/core/src/root/ServerService.ts @@ -33,8 +33,7 @@ export class ServerService { this.settings.clear(); parseJSONFlat( serverConfig.productConfiguration, - this.settings.setSelfValue.bind(this.settings), - undefined + this.settings.setSelfValue.bind(this.settings) ); } return serverConfig; diff --git a/webapp/packages/core/src/sdk/GQLErrorCatcher.ts b/webapp/packages/core/src/sdk/GQLErrorCatcher.ts new file mode 100644 index 0000000000..380d2f3e87 --- /dev/null +++ b/webapp/packages/core/src/sdk/GQLErrorCatcher.ts @@ -0,0 +1,34 @@ +/* + * cloudbeaver - Cloud Database Manager + * Copyright (C) 2020 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 { observable, action } from 'mobx'; + +import { GQLError } from './GQLError'; + +export class GQLErrorCatcher { + @observable hasDetails = false + @observable responseMessage: string | null = null + @observable exception: GQLError | null = null; + + @action catch(exception: any): boolean { + if (exception instanceof GQLError) { + this.responseMessage = exception.errorText; + this.hasDetails = exception.hasDetails(); + this.exception = exception; + return true; + } + this.clear(); + return false; + } + + @action clear() { + this.hasDetails = false; + this.responseMessage = null; + this.exception = null; + } +} diff --git a/webapp/packages/core/src/sdk/index.ts b/webapp/packages/core/src/sdk/index.ts index 11de1dd097..2b9ebec5ed 100644 --- a/webapp/packages/core/src/sdk/index.ts +++ b/webapp/packages/core/src/sdk/index.ts @@ -5,3 +5,4 @@ export * from './GQLError'; export * from './ServerInternalError'; export * from './CachedResource'; export * from './EServerErrorCode'; +export * from './GQLErrorCatcher'; diff --git a/webapp/packages/custom-connection-plugin/src/CustomConnection/ConnectionFormDialog/ConnectionFormDialog.tsx b/webapp/packages/custom-connection-plugin/src/CustomConnection/ConnectionFormDialog/ConnectionFormDialog.tsx index 0ef2030383..b276525b51 100644 --- a/webapp/packages/custom-connection-plugin/src/CustomConnection/ConnectionFormDialog/ConnectionFormDialog.tsx +++ b/webapp/packages/custom-connection-plugin/src/CustomConnection/ConnectionFormDialog/ConnectionFormDialog.tsx @@ -24,7 +24,6 @@ import { ConnectionFormDialogController } from './ConnectionFormDialogController import { ConnectionFormDialogFooter } from './ConnectionFormDialogFooter'; import { DriverProperties } from './DriverProperties/DriverProperties'; - const styles = composes( css` BaseTab { @@ -149,10 +148,10 @@ export const ConnectionFormDialog = observer( /> - {controller.responseMessage && ( + {controller.error.responseMessage && ( )} diff --git a/webapp/packages/custom-connection-plugin/src/CustomConnection/ConnectionFormDialog/ConnectionFormDialogController.ts b/webapp/packages/custom-connection-plugin/src/CustomConnection/ConnectionFormDialog/ConnectionFormDialogController.ts index 26648662a4..4c04ceaed7 100644 --- a/webapp/packages/custom-connection-plugin/src/CustomConnection/ConnectionFormDialog/ConnectionFormDialogController.ts +++ b/webapp/packages/custom-connection-plugin/src/CustomConnection/ConnectionFormDialog/ConnectionFormDialogController.ts @@ -12,7 +12,7 @@ import { DBDriver, ErrorDetailsDialog } from '@dbeaver/core/app'; import { injectable, IInitializableController, IDestructibleController } from '@dbeaver/core/di'; import { CommonDialogService } from '@dbeaver/core/dialogs'; import { NotificationService } from '@dbeaver/core/eventsLog'; -import { ConnectionConfig, GQLError } from '@dbeaver/core/sdk'; +import { ConnectionConfig, GQLErrorCatcher } from '@dbeaver/core/sdk'; import { CustomConnectionService } from '../../CustomConnectionService'; @@ -39,10 +39,7 @@ implements IInitializableController, IDestructibleController { url: '', properties: {}, } - @observable hasDetails = false - @observable responseMessage: string | null = null - - private exception: GQLError | null = null; + readonly error = new GQLErrorCatcher(); private onClose!: () => void private isDistructed = false; @@ -70,7 +67,7 @@ implements IInitializableController, IDestructibleController { onTestConnection = async () => { this.isConnecting = true; - this.clearError(); + this.error.clear(); try { await this.customConnectionService.testConnectionAsync(this.getConnectionConfig()); @@ -84,7 +81,7 @@ implements IInitializableController, IDestructibleController { onCreateConnection = async () => { this.isConnecting = true; - this.clearError(); + this.error.clear(); try { const connection = await this.customConnectionService.createConnectionAsync(this.getConnectionConfig()); @@ -98,8 +95,8 @@ implements IInitializableController, IDestructibleController { } onShowDetails = () => { - if (this.exception) { - this.commonDialogService.open(ErrorDetailsDialog, this.exception); + if (this.error.exception) { + this.commonDialogService.open(ErrorDetailsDialog, this.error.exception); } } @@ -144,12 +141,6 @@ implements IInitializableController, IDestructibleController { this.config.properties = {}; } - private clearError() { - this.responseMessage = null; - this.hasDetails = false; - this.exception = null; - } - /** * Creates connection name based on connection url * @param defaultName default connection name if url parsing failed @@ -175,11 +166,7 @@ implements IInitializableController, IDestructibleController { } private showError(exception: Error, message: string) { - if (exception instanceof GQLError && !this.isDistructed) { - this.responseMessage = exception.errorText; - this.hasDetails = exception.hasDetails(); - this.exception = exception; - } else { + if (!this.error.catch(exception) || this.isDistructed) { this.notificationService.logException(exception, message); } } diff --git a/webapp/packages/custom-connection-plugin/src/CustomConnection/CustomConnectionDialog.tsx b/webapp/packages/custom-connection-plugin/src/CustomConnection/CustomConnectionDialog.tsx index 8df2c23d58..b0d07b4909 100644 --- a/webapp/packages/custom-connection-plugin/src/CustomConnection/CustomConnectionDialog.tsx +++ b/webapp/packages/custom-connection-plugin/src/CustomConnection/CustomConnectionDialog.tsx @@ -9,7 +9,7 @@ import { observer } from 'mobx-react'; import { useController } from '@dbeaver/core/di'; -import { DialogComponent, DialogComponentProps } from '@dbeaver/core/dialogs'; +import { DialogComponent } from '@dbeaver/core/dialogs'; import { useTranslate } from '@dbeaver/core/localization'; import { ConnectionFormDialog } from './ConnectionFormDialog/ConnectionFormDialog'; @@ -17,7 +17,7 @@ import { CustomConnectionController, ConnectionStep } from './CustomConnectionCo import { DriverSelectorDialog } from './DriverSelectorDialog/DriverSelectorDialog'; export const CustomConnectionDialog: DialogComponent = observer( - function CustomConnectionDialog(props: DialogComponentProps) { + function CustomConnectionDialog(props) { const controller = useController(CustomConnectionController); let title = useTranslate('basicConnection_connectionDialog_newConnection');