Skip to content

Commit

Permalink
fix: Improve error message for disconnected connections (#678)
Browse files Browse the repository at this point in the history
`this._done` is used from the default `tx` behavior. This can contain
`committed` or `rolled back`. If `this._done` is not set it means the
connection was never connected.
  • Loading branch information
BobdenOs authored Jun 12, 2024
1 parent 92cf307 commit eb4ef37
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class HANAService extends SQLService {
}

ensureDBC() {
return this.dbc || cds.error`Database is disconnected`
return this.dbc || cds.error`Database connection is ${this._done || 'disconnected'}`
}

async set(variables) {
Expand Down Expand Up @@ -137,9 +137,13 @@ class HANAService extends SQLService {

// REVISIT: add prepare options when param:true is used
const sqlScript = isLockQuery || isSimple ? sql : this.wrapTemporary(temporary, withclause, blobs)
let rows = (values?.length || blobs.length > 0)
? await (await this.prepare(sqlScript, blobs.length)).all(values || [])
: await this.exec(sqlScript)
let rows
if (values?.length || blobs.length > 0) {
const ps = await this.prepare(sqlScript, blobs.length)
rows = this.ensureDBC() && await ps.all(values || [])
} else {
rows = await this.exec(sqlScript)
}

if (isLockQuery) {
// Fetch actual locked results
Expand Down Expand Up @@ -167,9 +171,9 @@ class HANAService extends SQLService {
// HANA driver supports batch execution
const results = await (entries
? HANAVERSION <= 2
? entries.reduce((l, c) => l.then(() => ps.run(c)), Promise.resolve(0))
: entries.length > 1 ? await ps.runBatch(entries) : await ps.run(entries[0])
: ps.run())
? entries.reduce((l, c) => l.then(() => this.ensureDBC() && ps.run(c)), Promise.resolve(0))
: entries.length > 1 ? this.ensureDBC() && await ps.runBatch(entries) : this.ensureDBC() && await ps.run(entries[0])
: this.ensureDBC() && ps.run())
return new this.class.InsertResults(cqn, results)
} catch (err) {
throw _not_unique(err, 'ENTITY_ALREADY_EXISTS')
Expand Down Expand Up @@ -1104,7 +1108,7 @@ class HANAService extends SQLService {
const { sql, values } = this.cqn2sql(query, data)
try {
let ps = await this.prepare(sql)
return (await ps.run(values)).changes
return (this.ensureDBC() && await ps.run(values)).changes
} catch (err) {
// Allow drop to fail when the view or table does not exist
if (event === 'DROP ENTITY' && (err.code === 259 || err.code === 321)) {
Expand Down Expand Up @@ -1134,7 +1138,7 @@ class HANAService extends SQLService {
async onCall({ query, data }, name, schema) {
const outParameters = await this._getProcedureMetadata(name, schema)
const ps = await this.prepare(query)
return ps.proc(data, outParameters)
return this.ensureDBC() && ps.proc(data, outParameters)
}

async onPlainSQL(req, next) {
Expand Down Expand Up @@ -1201,7 +1205,7 @@ class HANAService extends SQLService {
this.dbc = con

const stmt = await this.dbc.prepare(createContainerDatabase)
const res = await stmt.run([creds.user, creds.password, creds.containerGroup, !clean])
const res = this.ensureDBC() && await stmt.run([creds.user, creds.password, creds.containerGroup, !clean])
res && DEBUG?.(res.changes.map(r => r.MESSAGE).join('\n'))
} finally {
if (this.dbc) {
Expand Down Expand Up @@ -1243,7 +1247,7 @@ class HANAService extends SQLService {
this.dbc = con

const stmt = await this.dbc.prepare(createContainerTenant.replaceAll('{{{GROUP}}}', creds.containerGroup))
const res = await stmt.run([creds.user, creds.password, creds.schema, !clean])
const res = this.ensureDBC() && await stmt.run([creds.user, creds.password, creds.schema, !clean])
res && DEBUG?.(res.changes.map?.(r => r.MESSAGE).join('\n'))
} finally {
await this.dbc.disconnect()
Expand Down

0 comments on commit eb4ef37

Please sign in to comment.