Skip to content

Instantly share code, notes, and snippets.

@giladno
Last active November 25, 2017 16:55
Show Gist options
  • Save giladno/38934f5b550bd7bbf5fac05bb1e80861 to your computer and use it in GitHub Desktop.
Save giladno/38934f5b550bd7bbf5fac05bb1e80861 to your computer and use it in GitHub Desktop.
ewrap all
'use strict';
const express = require('express');
const app = (()=>{
const ewrap = f=>{
if (f.length>3)
return f;
return (req, res, next)=>(async ()=>f(req, res, next))().catch(next);
};
let app = express();
for (let fn of ['use', 'all', 'get', 'post', 'put', 'delete', 'head', 'options'])
{
let orig = app[fn].bind(app);
app[fn] = (...args)=>orig(...args.map(arg=>typeof arg=='function' ? ewrap(arg) : arg));
}
return app;
})();
app.all('/test', async (req, res)=>{
res.send('test');
});
app.all('/error', async (req, res)=>{
throw new Error('error');
});
app.use((err, req, res, next)=>{
console.log(err.stack);
res.status(500).send('Server Error');
});
app.listen(8080);
@wizardnet972
Copy link

wizardnet972 commented Nov 25, 2017

app.all('/test', async (req, res)=>{
  const promise = await new Promise(function (resolve, reject) {
        setTimeout(function () {
            reject(new Error('this is a test err'));
        }, 3000);
    });
});

this is not working..

@wizardnet972
Copy link

(node:23932) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:23932) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment