0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

🐛 Fixed error handling in async helpers

refs #8703

- Always always call cb() even if we get an error!
- Ensure the error is handled, and converted to a GhostError if not already
- If we're in development mode, render the error. Else render nothing.
This commit is contained in:
Hannah Wolfe 2017-08-27 17:06:44 +02:00 committed by Katharina Irrgang
parent 0c4fcfdbe3
commit b6b2930d15

View file

@ -1,6 +1,8 @@
var hbs = require('../themes/engine'),
Promise = require('bluebird'),
errors = require('../errors');
config = require('../config'),
errors = require('../errors'),
logging = require('../logging');
// Register an async handlebars helper for a given handlebars instance
function asyncHelperWrapper(hbs, name, fn) {
@ -12,13 +14,18 @@ function asyncHelperWrapper(hbs, name, fn) {
}
// Wrap the function passed in with a when.resolve so it can return either a promise or a value
Promise.resolve(fn.call(this, context, options)).then(function (result) {
Promise.resolve(fn.call(this, context, options)).then(function asyncHelperSuccess(result) {
cb(result);
}).catch(function (err) {
throw new errors.IncorrectUsageError({
err: err,
context: 'registerAsyncThemeHelper: ' + name
});
}).catch(function asyncHelperError(err) {
var wrappedErr = err instanceof errors.GhostError ? err : new errors.IncorrectUsageError({
err: err,
context: 'registerAsyncThemeHelper: ' + name
}),
result = config.get('env') === 'development' ? wrappedErr : '';
logging.error(wrappedErr);
cb(new hbs.SafeString(result));
});
});
}