Skip to content
This repository was archived by the owner on Mar 7, 2024. It is now read-only.

Commit d442ff2

Browse files
authored
Catch errors when executing query
1 parent 38a2c6e commit d442ff2

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

index.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,24 +243,25 @@ const executeQuery = async (query: string, values: any[], config: PostgresMeta['
243243
database: config.dbName,
244244
port: parseInt(config.port),
245245
}
246-
const pgClient = new Client({
247-
...basicConnectionOptions,
248-
ssl: {
249-
rejectUnauthorized: config.hasSelfSignedCert === 'No',
250-
},
251-
})
252-
253-
await pgClient.connect()
254-
246+
255247
let error: Error | null = null
248+
249+
let pgClient = Client | null = null
256250
try {
251+
pgClient = new Client({
252+
...basicConnectionOptions,
253+
ssl: {
254+
rejectUnauthorized: config.hasSelfSignedCert === 'No',
255+
},
256+
})
257+
258+
await pgClient.connect()
257259
await pgClient.query(query, values)
258260
} catch (err) {
259261
error = err as Error
262+
} finally {
263+
await pgClient?.end()
260264
}
261-
262-
await pgClient.end()
263-
264265
return error
265266
}
266267

0 commit comments

Comments
 (0)