Skip to content

Commit 1a090e3

Browse files
committed
Release client if unexpected error occurs. Handle pool errors.
1 parent 3af0251 commit 1a090e3

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tinypg",
3-
"version": "2.0.0-alpha.12",
3+
"version": "2.0.0-alpha.13",
44
"description": "Easy way to call sql files using postgres.",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",

src/tiny.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export class TinyPg {
4343

4444
this.pool = new Pg.Pool(pool_config)
4545

46+
this.pool.on('error', (error) => {
47+
TINYPG_LOG && Util.Log('Error with idle client in pool.', error)
48+
})
49+
4650
this.sql_files = P.parseFiles([].concat(this.options.root_dir))
4751

4852
this.sql_db_calls = _.keyBy(_.map(this.sql_files, sql_file => {
@@ -185,8 +189,12 @@ export class TinyPg {
185189

186190
this.events.emit('query', query_context)
187191

188-
const callComplete = (error: Error, data: T.Result<T>) => {
189-
client.release()
192+
const callComplete = _.once((error: Error, data: T.Result<T>) => {
193+
if (error && (!error['code'] || _.startsWith(error['code'], '57P'))) {
194+
(<any>client).release(error)
195+
} else {
196+
client.release()
197+
}
190198

191199
const end_at = Date.now()
192200

@@ -198,9 +206,9 @@ export class TinyPg {
198206
})
199207

200208
this.events.emit('result', complete_context)
201-
}
209+
})
202210

203-
return Promise.resolve()
211+
const query_promise = Promise.resolve()
204212
.then(() => {
205213
TINYPG_LOG && Util.Log('executing', db_call.config.name)
206214

@@ -226,6 +234,8 @@ export class TinyPg {
226234
}
227235
})
228236
})
237+
238+
return query_promise
229239
.then(result => {
230240
callComplete(null, result)
231241
return result
@@ -234,8 +244,10 @@ export class TinyPg {
234244
callComplete(error, null)
235245

236246
const tiny_error = new T.TinyPgError(error.message)
247+
237248
tiny_error.stack = stack_trace_accessor.stack
238249
tiny_error.queryContext = query_context
250+
239251
throw this.options.error_transformer(tiny_error)
240252
})
241253
})

0 commit comments

Comments
 (0)