propagate errors coreectly for datasource plus

This commit is contained in:
Peter Clement 2023-06-27 15:27:56 +01:00
parent 82bafacd72
commit 46a9142676
3 changed files with 40 additions and 8 deletions

View File

@ -10,6 +10,7 @@ import {
DatasourcePlus,
DatasourceFeature,
ConnectionInfo,
SourceName,
} from "@budibase/types"
import {
getSqlQuery,
@ -20,6 +21,7 @@ import {
} from "./utils"
import Sql from "./base/sql"
import { MSSQLTablesResponse, MSSQLColumn } from "./base/types"
import { getReadableErrorMessage } from "./base/errorMapping"
const sqlServer = require("mssql")
const DEFAULT_SCHEMA = "dbo"
@ -177,9 +179,16 @@ class SqlServerIntegration extends Sql implements DatasourcePlus {
? `${query.sql}; SELECT SCOPE_IDENTITY() AS id;`
: query.sql
return await request.query(sql)
} catch (err) {
// @ts-ignore
throw new Error(err)
} catch (err: any) {
let readableMessage = getReadableErrorMessage(
SourceName.SQL_SERVER,
err.errno
)
if (readableMessage) {
throw new Error(readableMessage)
} else {
throw new Error(err.message as string)
}
}
}

View File

@ -9,6 +9,7 @@ import {
DatasourcePlus,
DatasourceFeature,
ConnectionInfo,
SourceName,
} from "@budibase/types"
import {
getSqlQuery,
@ -21,7 +22,7 @@ import dayjs from "dayjs"
import { NUMBER_REGEX } from "../utilities"
import Sql from "./base/sql"
import { MySQLColumn } from "./base/types"
import { getReadableErrorMessage } from "./base/errorMapping"
import mysql from "mysql2/promise"
interface MySQLConfig extends mysql.ConnectionOptions {
@ -174,7 +175,12 @@ class MySQLIntegration extends Sql implements DatasourcePlus {
)
response.connected = result?.checkRes == 2
} catch (e: any) {
response.error = e.message as string
let readableMessage = getReadableErrorMessage(SourceName.MYSQL, e.errno)
if (readableMessage) {
response.error = readableMessage
} else {
response.error = e.message as string
}
}
return response
}
@ -213,6 +219,13 @@ class MySQLIntegration extends Sql implements DatasourcePlus {
// Node MySQL is callback based, so we must wrap our call in a promise
const response = await this.client!.query(query.sql, bindings)
return response[0]
} catch (err: any) {
let readableMessage = getReadableErrorMessage(SourceName.MYSQL, err.errno)
if (readableMessage) {
throw new Error(readableMessage)
} else {
throw new Error(err.message as string)
}
} finally {
if (opts?.connect && this.client) {
await this.disconnect()

View File

@ -8,6 +8,7 @@ import {
DatasourcePlus,
DatasourceFeature,
ConnectionInfo,
SourceName,
} from "@budibase/types"
import {
getSqlQuery,
@ -21,6 +22,7 @@ import { PostgresColumn } from "./base/types"
import { escapeDangerousCharacters } from "../utilities"
import { Client, ClientConfig, types } from "pg"
import { getReadableErrorMessage } from "./base/errorMapping"
// Return "date" and "timestamp" types as plain strings.
// This lets us reference the original stored timezone.
@ -182,6 +184,7 @@ class PostgresIntegration extends Sql implements DatasourcePlus {
await this.openConnection()
response.connected = true
} catch (e: any) {
console.log(e)
response.error = e.message as string
} finally {
await this.closeConnection()
@ -240,10 +243,17 @@ class PostgresIntegration extends Sql implements DatasourcePlus {
}
try {
return await client.query(query.sql, query.bindings || [])
} catch (err) {
} catch (err: any) {
await this.closeConnection()
// @ts-ignore
throw new Error(err)
let readableMessage = getReadableErrorMessage(
SourceName.POSTGRES,
err.errno
)
if (readableMessage) {
throw new Error(readableMessage)
} else {
throw new Error(err.message as string)
}
} finally {
if (close) {
await this.closeConnection()