diff --git a/packages/builder/src/components/backend/Datasources/TableImportSelection/tableSelectionStore.js b/packages/builder/src/components/backend/Datasources/TableImportSelection/tableSelectionStore.js index 6235ea397a..5c2ea9767c 100644 --- a/packages/builder/src/components/backend/Datasources/TableImportSelection/tableSelectionStore.js +++ b/packages/builder/src/components/backend/Datasources/TableImportSelection/tableSelectionStore.js @@ -1,6 +1,6 @@ import { derived, writable, get } from "svelte/store" import { keepOpen, notifications } from "@budibase/bbui" -import { datasources, ImportTableError, tables } from "stores/backend" +import { datasources, tables } from "stores/backend" export const createTableSelectionStore = (integration, datasource) => { const tableNamesStore = writable([]) @@ -30,12 +30,7 @@ export const createTableSelectionStore = (integration, datasource) => { notifications.success(`Tables fetched successfully.`) await onComplete() } catch (err) { - if (err instanceof ImportTableError) { - errorStore.set(err) - } else { - notifications.error("Error fetching tables.") - } - + errorStore.set(err) return keepOpen } } diff --git a/packages/builder/src/stores/backend/datasources.js b/packages/builder/src/stores/backend/datasources.js index cec2f4eac2..9265f34b96 100644 --- a/packages/builder/src/stores/backend/datasources.js +++ b/packages/builder/src/stores/backend/datasources.js @@ -9,15 +9,23 @@ import { API } from "api" import { DatasourceFeature } from "@budibase/types" import { TableNames } from "constants" -export class ImportTableError extends Error { - constructor(message) { - super(message) - const [title, description] = message.split(" - ") +class TableImportError extends Error { + constructor(errors) { + super(`Error importing tables: ${Object.keys(errors).join(", ")}`) + this.name = "TableImportError" + this.errors = errors + } - this.name = "TableSelectionError" - // Capitalize the first character of both the title and description - this.title = title[0].toUpperCase() + title.substr(1) - this.description = description[0].toUpperCase() + description.substr(1) + get title() { + return `Error fetching tables` + } + + get description() { + let message = "" + for (const key in this.errors) { + message += `${key}: ${this.errors[key]}\n` + } + return message } } @@ -78,9 +86,9 @@ export function createDatasourcesStore() { } const updateDatasource = response => { - const { datasource, error } = response - if (error) { - store.update(state => ({ ...state })) + const { datasource, errors } = response + if (errors && Object.keys(errors).length > 0) { + throw new TableImportError(errors) } replaceDatasource(datasource._id, datasource) select(datasource._id) @@ -88,20 +96,11 @@ export function createDatasourcesStore() { } const updateSchema = async (datasource, tablesFilter) => { - try { - const response = await API.buildDatasourceSchema({ - datasourceId: datasource?._id, - tablesFilter, - }) - updateDatasource(response) - } catch (e) { - // buildDatasourceSchema call returns user presentable errors with two parts divided with a " - ". - if (e.message.split(" - ").length === 2) { - throw new ImportTableError(e.message) - } else { - throw e - } - } + const response = await API.buildDatasourceSchema({ + datasourceId: datasource?._id, + tablesFilter, + }) + updateDatasource(response) } const sourceCount = source => { diff --git a/packages/builder/src/stores/backend/index.js b/packages/builder/src/stores/backend/index.js index 278e43c1ed..3781e2ab92 100644 --- a/packages/builder/src/stores/backend/index.js +++ b/packages/builder/src/stores/backend/index.js @@ -4,7 +4,7 @@ export { views } from "./views" export { viewsV2 } from "./viewsV2" export { permissions } from "./permissions" export { roles } from "./roles" -export { datasources, ImportTableError } from "./datasources" +export { datasources } from "./datasources" export { integrations } from "./integrations" export { sortedIntegrations } from "./sortedIntegrations" export { queries } from "./queries"