From 882234223141f8d531bb73dbd269129dfd966d06 Mon Sep 17 00:00:00 2001 From: guerler Date: Tue, 26 Nov 2024 12:01:37 +0300 Subject: [PATCH] Improve error handling --- package.json | 4 ++-- src/Plugin.vue | 6 ++++-- src/api/client.ts | 8 ++++++-- src/api/datasets.ts | 20 +++++++++----------- src/api/visualizations.ts | 17 +++++++++++++---- src/types.ts | 4 ++-- src/utilities/parseColumns.ts | 7 +------ src/utilities/parsePlugin.ts | 5 +---- src/utilities/sleep.ts | 6 ++++++ 9 files changed, 44 insertions(+), 33 deletions(-) create mode 100644 src/utilities/sleep.ts diff --git a/package.json b/package.json index acaee60..10cb12a 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "charts" ], "license": "MIT", - "version": "0.0.35", + "version": "0.0.37", "type": "module", "main": "./dist/galaxy-charts.umd.cjs", "module": "./dist/galaxy-charts.js", @@ -30,7 +30,7 @@ "build:types": "vue-tsc --declaration --emitDeclarationOnly --outDir dist", "postbuild": "npm run build:types", "preview": "vite preview", - "prettier": "prettier --write 'package.json' '*.js' 'src/**/*.js' 'src/**/*.vue'", + "prettier": "prettier --write 'package.json' '*.js' 'src/**/*.js' 'src/**/*.ts' 'src/**/*.vue'", "test": "vitest" }, "devDependencies": { diff --git a/src/Plugin.vue b/src/Plugin.vue index a01e3b0..801e800 100644 --- a/src/Plugin.vue +++ b/src/Plugin.vue @@ -21,10 +21,11 @@ const viewport = ref(null); const columnsStore = useColumnsStore(); +const columnsList = ref(); + async function render() { /** Place your render function here! */ - const columnsList = await columnsStore.fetchColumns(props.datasetId, props.tracks, ["x", "y", "z"]); - console.log(columnsList); + columnsList.value = await columnsStore.fetchColumns(props.datasetId, props.tracks, ["x", "y", "z"]); } function onSave() { @@ -52,6 +53,7 @@ watch(
Settings: {{ settings }}
Tracks: {{ tracks }}
+
{{ columnsList }}
diff --git a/src/api/client.ts b/src/api/client.ts index 0f0febb..d9a0d5b 100644 --- a/src/api/client.ts +++ b/src/api/client.ts @@ -10,8 +10,12 @@ async function fetchApi(path: string, options: RequestInit): Promise<{ data: any headers: { "Content-Type": "application/json" }, ...options, }); - const data = await response.json(); - return { data, response }; + if (response.ok) { + const data = await response.json(); + return { data, response }; + } else { + rethrowSimple(response); + } } catch (err) { rethrowSimple(err); } diff --git a/src/api/datasets.ts b/src/api/datasets.ts index ac3e498..5596cdb 100644 --- a/src/api/datasets.ts +++ b/src/api/datasets.ts @@ -1,17 +1,17 @@ import { GalaxyApi } from "@/api/client"; +import { rethrowSimple } from "@/utilities/simpleError"; export async function datasetsGetColumns(datasetId: string, columnList: string[]): Promise { - try { - const params = new URLSearchParams({ - data_type: "raw_data", - provider: "dataset-column", - indeces: columnList.toString(), - }).toString(); + const params = new URLSearchParams({ + data_type: "raw_data", + provider: "dataset-column", + indeces: columnList.toString(), + }).toString(); + try { const { data } = await GalaxyApi().GET(`/api/datasets/${datasetId}?${params}`); const columnLength = columnList.length; const results: any[][] = new Array(columnLength).fill(null).map(() => []); - for (const row of data.data) { for (const j in row) { const index = Number(j); @@ -21,11 +21,9 @@ export async function datasetsGetColumns(datasetId: string, columnList: string[] } } } - return results; - } catch (err) { - console.error(err); - return undefined; + } catch (e) { + rethrowSimple(e); } } diff --git a/src/api/visualizations.ts b/src/api/visualizations.ts index 1467018..8636729 100644 --- a/src/api/visualizations.ts +++ b/src/api/visualizations.ts @@ -1,6 +1,6 @@ -import { rethrowSimple } from "@/utilities/simpleError"; import { GalaxyApi } from "@/api/client"; -import { InputValuesType } from "@/types" +import { InputValuesType } from "@/types"; +import { rethrowSimple } from "@/utilities/simpleError"; interface VisualizationConfig { dataset_id: string; @@ -8,7 +8,11 @@ interface VisualizationConfig { tracks: Array; } -export async function visualizationsCreate(type: string, title: string, config: VisualizationConfig): Promise { +export async function visualizationsCreate( + type: string, + title: string, + config: VisualizationConfig, +): Promise { try { const { data } = await GalaxyApi().POST("/api/visualizations", { type, @@ -21,7 +25,12 @@ export async function visualizationsCreate(type: string, title: string, config: } } -export async function visualizationsSave(type: string, id: string | null, title: string, config: VisualizationConfig): Promise { +export async function visualizationsSave( + type: string, + id: string | null, + title: string, + config: VisualizationConfig, +): Promise { try { if (id) { await visualizationsUpdate(id, title, config); diff --git a/src/types.ts b/src/types.ts index a6be4da..3738a77 100644 --- a/src/types.ts +++ b/src/types.ts @@ -45,9 +45,9 @@ export interface PluginConfigType { export interface PluginIncomingType { root?: string; - visualization_config?: PluginConfigType, + visualization_config?: PluginConfigType; visualization_id?: string; - visualization_plugin?: PluginType, + visualization_plugin?: PluginType; visualization_title?: string; } diff --git a/src/utilities/parseColumns.ts b/src/utilities/parseColumns.ts index 674e070..761c26e 100644 --- a/src/utilities/parseColumns.ts +++ b/src/utilities/parseColumns.ts @@ -7,12 +7,7 @@ interface Column { value: string; } -export function parseColumns( - dataset: Dataset, - isAuto: boolean, - isText: boolean, - isNumber: boolean -): Array { +export function parseColumns(dataset: Dataset, isAuto: boolean, isText: boolean, isNumber: boolean): Array { const columns: Array = []; if (isAuto) { diff --git a/src/utilities/parsePlugin.ts b/src/utilities/parsePlugin.ts index 70e2a4e..74dde2d 100644 --- a/src/utilities/parsePlugin.ts +++ b/src/utilities/parsePlugin.ts @@ -9,10 +9,7 @@ interface ParsedPlugin { } // Parse plugin either from incoming object or XML -export async function parsePlugin( - plugin: PluginType, - config: PluginConfigType = {} -): Promise { +export async function parsePlugin(plugin: PluginType, config: PluginConfigType = {}): Promise { const settings = parseValues(plugin.settings, config.settings); const specs = plugin.specs; const tracks = parseTracks(plugin.tracks, config.tracks); diff --git a/src/utilities/sleep.ts b/src/utilities/sleep.ts new file mode 100644 index 0000000..302d6df --- /dev/null +++ b/src/utilities/sleep.ts @@ -0,0 +1,6 @@ +const WAITTIME = 2000; + +/** Wait helper */ +export function sleep(wt = WAITTIME) { + return new Promise((resolve) => setTimeout(resolve, wt)); +}