mattstyles / koa-socket

Sugar for connecting socket.io to a koa instance
242 stars 50 forks source link

Middleware - error handling #27

Open javorka opened 7 years ago

javorka commented 7 years ago

Hi, I'm trying to create simple error handling middleware like this:

io.use( async ( ctx, next ) => {
  try {
    await next();
  } catch (err) {
    console.log('OUCH, ERROR OCCURRED', err.message);
  }
});

I'm using async/await stuff (on latest Node - without babel or harmony flag), but I still get UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property ...

I also tried your provided example of middleware - logging response time, and it works. Please, can you help me with this issue?

lucasmacosta commented 7 years ago

+1 I have the same issue, composing the error handler with others middlewares using koa-compose and using that as event handler works but by itself it does not.