Skip to content

Commit

Permalink
fix: show error message when user meet 403 error back from server
Browse files Browse the repository at this point in the history
Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll committed Oct 17, 2024
1 parent 1d240d5 commit 8d3cbbd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/datasources/fetchApi.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 || '');
};

Expand Down
28 changes: 16 additions & 12 deletions src/store/connectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 8d3cbbd

Please sign in to comment.