Last active
February 1, 2018 02:58
-
-
Save ei-grad/0e8ea9f64a197e0a95f9 to your computer and use it in GitHub Desktop.
Exception handling in async/await sailsjs controllers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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