vkarpov15 / awaitjs-express

Write Express middleware and route handlers using async/await
Apache License 2.0
125 stars 13 forks source link

handle errors in async middleware mounted on a route? #5

Closed peterwhitesell closed 5 years ago

peterwhitesell commented 5 years ago

It would be nice to handle errors in async middleware that are mounted on a route as well. Currently the following middleware error would not be handled by the error middleware

const express = require('express');
const { decorateApp } = require('@awaitjs/express');
const app = decorateApp(express());

async function middleware(req, res, next) {
  throw new Error('From Middleware');
}

async function handler(req, res, next) {
  res.send('OK');
}

app.getAsync('/test', middleware, handler);

app.use(function(error, req, res, next) {
  res.send(error.message);
});

app.listen(3000);

It looks like currently only the last argument to [method]Async is wrapped if it's a function. Would it be easy enough to do the same for all arguments?

vkarpov15 commented 5 years ago

Yes it should be pretty easy, will do :+1:

vkarpov15 commented 5 years ago

Fixed and released 0.2.0