This is an automated email from the ASF dual-hosted git repository.

mintsweet pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new 153e0ed22 fix(config-ui): missed catch the test connection error 
(#6498)
153e0ed22 is described below

commit 153e0ed22674afea9dcad7eb2919fd9120bc8e7b
Author: 青湛 <[email protected]>
AuthorDate: Thu Nov 23 12:11:21 2023 +1300

    fix(config-ui): missed catch the test connection error (#6498)
    
    * fix(config-ui): missed catch the test connection error
    
    * fix(config-ui): remove useless console.log
---
 config-ui/src/features/connections/slice.ts        | 46 ++++++++++++++--------
 .../plugins/components/connection-status/index.tsx |  3 +-
 config-ui/src/utils/operator.ts                    | 14 +++++--
 3 files changed, 41 insertions(+), 22 deletions(-)

diff --git a/config-ui/src/features/connections/slice.ts 
b/config-ui/src/features/connections/slice.ts
index af62d5b49..d8be32f64 100644
--- a/config-ui/src/features/connections/slice.ts
+++ b/config-ui/src/features/connections/slice.ts
@@ -54,7 +54,6 @@ export const init = createAsyncThunk('connections/init', 
async (plugins: string[
       .filter((plugin) => plugin === 'webhook')
       .map(async () => {
         const webhooks = await API.plugin.webhook.list();
-        console.log(webhooks);
         return webhooks.map((webhook) => transformWebhook(webhook));
       }),
   );
@@ -93,22 +92,29 @@ export const removeConnection = createAsyncThunk(
 
 export const testConnection = createAsyncThunk(
   'connections/testConnection',
-  async ({ unique, plugin, endpoint, proxy, token, username, password, 
authMethod, secretKey, appId }: IConnection) => {
-    const res = await API.connection.test(plugin, {
-      endpoint,
-      proxy,
-      token,
-      username,
-      password,
-      authMethod,
-      secretKey,
-      appId,
-    });
-
-    return {
-      unique,
-      status: res.success ? IConnectionStatus.ONLINE : 
IConnectionStatus.OFFLINE,
-    };
+  async (
+    { unique, plugin, endpoint, proxy, token, username, password, authMethod, 
secretKey, appId }: IConnection,
+    { rejectWithValue },
+  ) => {
+    try {
+      const res = await API.connection.test(plugin, {
+        endpoint,
+        proxy,
+        token,
+        username,
+        password,
+        authMethod,
+        secretKey,
+        appId,
+      });
+
+      return {
+        unique,
+        status: res.success ? IConnectionStatus.ONLINE : 
IConnectionStatus.OFFLINE,
+      };
+    } catch (err: any) {
+      return rejectWithValue({ unique, response: err.response });
+    }
   },
 );
 
@@ -185,6 +191,12 @@ export const connectionsSlice = createSlice({
           existingConnection.status = action.payload.status;
         }
       })
+      .addCase(testConnection.rejected, (state, action) => {
+        const existingConnection = state.connections.find((cs) => cs.unique 
=== action.meta.arg.unique);
+        if (existingConnection) {
+          existingConnection.status = IConnectionStatus.OFFLINE;
+        }
+      })
       .addCase(addWebhook.fulfilled, (state, action) => {
         state.webhooks.push(action.payload.webhook);
       })
diff --git a/config-ui/src/plugins/components/connection-status/index.tsx 
b/config-ui/src/plugins/components/connection-status/index.tsx
index 136f9b59b..6bf893dac 100644
--- a/config-ui/src/plugins/components/connection-status/index.tsx
+++ b/config-ui/src/plugins/components/connection-status/index.tsx
@@ -22,6 +22,7 @@ import { useAppDispatch } from '@/app/hook';
 import { IconButton } from '@/components';
 import { testConnection } from '@/features/connections';
 import { IConnection, IConnectionStatus } from '@/types';
+import { operator } from '@/utils';
 
 const Wrapper = styled.div`
   display: inline-flex;
@@ -52,7 +53,7 @@ export const ConnectionStatus = ({ connection }: Props) => {
 
   const dispatch = useAppDispatch();
 
-  const handleTest = () => dispatch(testConnection(connection));
+  const handleTest = () => operator(() => 
dispatch(testConnection(connection)).unwrap());
 
   return (
     <Wrapper>
diff --git a/config-ui/src/utils/operator.ts b/config-ui/src/utils/operator.ts
index d78fea5d2..fbdb6e5ca 100644
--- a/config-ui/src/utils/operator.ts
+++ b/config-ui/src/utils/operator.ts
@@ -16,7 +16,7 @@
  *
  */
 
-import { toast } from '@/components';
+import { message } from 'antd';
 
 export type OperateConfig = {
   setOperating?: (success: boolean) => void;
@@ -40,16 +40,22 @@ export const operator = async <T>(request: () => 
Promise<T>, config?: OperateCon
   try {
     setOperating?.(true);
     const res = await request();
-    const message = formatMessage?.() ?? 'Operation successfully completed';
+    const content = formatMessage?.() ?? 'Operation successfully completed';
     if (!config?.hideToast) {
-      toast.success(message);
+      message.open({
+        type: 'success',
+        content,
+      });
     }
     return [true, res];
   } catch (err) {
     console.error('Operation failed.', err);
     const reason = formatReason?.(err) ?? (err as any).response?.data?.message 
?? 'Operation failed.';
     if (!config?.hideToast) {
-      toast.error(reason);
+      message.open({
+        type: 'error',
+        content: reason,
+      });
     }
 
     return [false, err];

Reply via email to