From 8d3cbbd5feb917e3c80e2ced235f9a89320784e2 Mon Sep 17 00:00:00 2001 From: seven Date: Fri, 18 Oct 2024 00:53:56 +0800 Subject: [PATCH] fix: show error message when user meet 403 error back from server Signed-off-by: seven --- src/datasources/fetchApi.ts | 4 ++++ src/store/connectionStore.ts | 28 ++++++++++++++++------------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/datasources/fetchApi.ts b/src/datasources/fetchApi.ts index b19de0f..e585342 100644 --- a/src/datasources/fetchApi.ts +++ b/src/datasources/fetchApi.ts @@ -1,6 +1,7 @@ import { buildAuthHeader, buildURL, CustomError, debug } from '../common'; import { lang } from '../lang'; import { invoke } from '@tauri-apps/api/tauri'; +import { get } from 'lodash'; type FetchApiOptions = { method: string; @@ -18,6 +19,9 @@ const handleFetch = (result: { data: unknown; status: number; details: string | if (result.status === 401) { throw new CustomError(result.status, lang.global.t('connection.unAuthorized')); } + if (result.status === 403) { + throw new CustomError(result.status, get(result, 'data.error.reason', result.details || '')); + } throw new CustomError(result.status, result.details || ''); }; diff --git a/src/store/connectionStore.ts b/src/store/connectionStore.ts index 66520a7..de417d5 100644 --- a/src/store/connectionStore.ts +++ b/src/store/connectionStore.ts @@ -94,18 +94,22 @@ export const useConnectionStore = defineStore('connectionStore', { async establishConnection(connection: Connection) { await this.testConnection(connection); const client = loadHttpClient(connection); - const data = (await client.get('/_cat/indices', 'format=json')) as Array<{ - [key: string]: string; - }>; - const indices = data.map((index: { [key: string]: string }) => ({ - ...index, - docs: { - count: parseInt(index['docs.count'], 10), - deleted: parseInt(index['docs.deleted'], 10), - }, - store: { size: index['store.size'] }, - })) as ConnectionIndex[]; - this.established = { ...connection, indices }; + try { + const data = (await client.get('/_cat/indices', 'format=json')) as Array<{ + [key: string]: string; + }>; + const indices = data.map((index: { [key: string]: string }) => ({ + ...index, + docs: { + count: parseInt(index['docs.count'], 10), + deleted: parseInt(index['docs.deleted'], 10), + }, + store: { size: index['store.size'] }, + })) as ConnectionIndex[]; + this.established = { ...connection, indices }; + } catch (err) { + this.established = { ...connection, indices: [] }; + } }, async fetchIndices() { if (!this.established) throw new Error('no connection established');