Skip to content
This repository has been archived by the owner on Mar 21, 2018. It is now read-only.

Capture connections errors #3

Open
romainberger opened this issue Mar 6, 2014 · 1 comment
Open

Capture connections errors #3

romainberger opened this issue Mar 6, 2014 · 1 comment

Comments

@romainberger
Copy link

Hi, first thanks for this module!

I was wondering if there is a way to catch errors when creating the connection. With co-pg you can wrap the code in a try/catch block, so I tried the same with koa-pg but it doesn't seem to work:

var database = process.env.DATABASE_URL || 'postgres://user@localhost:5432/database/'
try {
  app.use(koaPg(database))
}
catch (e) {
  console.log('error')
}

I've tried wrapping the koaPg(database) alone too but it's not better. Am I doing this wrong is this not possible at all with this module?

Thanks

@rxw1
Copy link

rxw1 commented Feb 21, 2015

Hi there,

afaik the koa way to catch errors goes like this:

app.use(function*(next) {
  try {
    yield next;
    console.log('db connect success');
  } catch (err) {
    console.error('db connect failure', err);
    throw new Error('DB FAILURE');
  }
});

app.use(pg('postgres://user@localhost/database'));

It must have something to with that try/catch is synchronous and yield/next (or promises) are asynchronous.

Here's a Gist: https://gist.github.com/rwilhelm/aab3270c7c8ffabb18d2

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants