Skip to content

Instantly share code, notes, and snippets.

@ei-grad
Last active February 1, 2018 02:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ei-grad/0e8ea9f64a197e0a95f9 to your computer and use it in GitHub Desktop.
Save ei-grad/0e8ea9f64a197e0a95f9 to your computer and use it in GitHub Desktop.
Exception handling in async/await sailsjs controllers
/**
* ListController
*
* @description :: Server-side logic for managing lists
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/
import _ from 'lodash';
class AsyncController {
constructor(handlers) {
var self = this;
_.each(handlers, function(handler, key) {
self[key] = async function (req, res) {
try {
await handler(req, res);
} catch (e) {
res.serverError(e);
}
};
});
}
}
export default new AsyncController({
index: async function (req, res) {
await new Promise((respond, reject) => {
reject(new Error("Error raised!"));
});
res.ok('ok');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment