Skip to content

Commit

Permalink
add connection test before establish connection
Browse files Browse the repository at this point in the history
Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll committed Dec 26, 2023
1 parent 13e62b7 commit cdd515c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
26 changes: 12 additions & 14 deletions src/store/connectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,19 @@ export const useConnectionStore = defineStore('connectionStore', {
this.connections = await storeAPI.get('connections', []);
},
async testConnection({ host, port }: Connection) {
{
try {
const result = await fetch(`${host}:${port}`, {
method: 'GET',
});
if (!result.ok) new CustomError(result.status, await result.json());
} catch (e) {
if (e instanceof CustomError) {
throw new CustomError(e.status, e.details);
}
if (e instanceof Error) {
throw new CustomError(500, e.message);
}
throw new CustomError(500, `unknown error, trace: ${JSON.stringify(e)}`);
try {
const result = await fetch(`${host}:${port}`, {
method: 'GET',
});
if (!result.ok) new CustomError(result.status, await result.json());
} catch (e) {
if (e instanceof CustomError) {
throw new CustomError(e.status, e.details);
}
if (e instanceof Error) {
throw new CustomError(500, e.message);
}
throw new CustomError(500, `unknown error, trace: ${JSON.stringify(e)}`);
}
},
saveConnection(connection: Connection) {
Expand Down
22 changes: 16 additions & 6 deletions src/views/connect/components/connect-list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import { MoreOutlined } from '@vicons/antd';
import { storeToRefs } from 'pinia';
import { useLang } from '../../../lang';
import { Connection, useConnectionStore } from '../../../store';
import { debug } from '../../../common/debug';
import { CustomError } from 'src/common/customError';
const emits = defineEmits(['edit-connect']);
Expand All @@ -49,7 +51,7 @@ const options = reactive([
{ key: 3, label: lang.t('connection.operations.remove') },
]);
const connectionStore = useConnectionStore();
const { fetchConnections, removeConnection, establishConnection } = connectionStore;
const { fetchConnections, removeConnection, establishConnection, testConnection } = connectionStore;
const { connections, established } = storeToRefs(connectionStore);
fetchConnections();
Expand All @@ -67,11 +69,19 @@ const handleSelect = (key: number, connection: Connection) => {
}
};
// TODO:connect to the item
const establishConnect = (connection: Connection) => {
establishConnection(connection);
// eslint-disable-next-line no-console
console.log(connection);
const establishConnect = async (connection: Connection) => {
try {
await testConnection(connection);
establishConnection(connection);
} catch (err) {
const error = err as CustomError;
message.error(`status: ${error.status}, details: ${error.details}`, {
closable: true,
keepAliveOnHover: true,
duration: 36000000,
});
debug('connect error');
}
};
// edit connect info
Expand Down

0 comments on commit cdd515c

Please sign in to comment.