2017-12-14 03:01:23 +01:00
|
|
|
var hbs = require('../services/themes/engine'),
|
2017-04-04 17:07:35 +01:00
|
|
|
Promise = require('bluebird'),
|
2019-06-19 11:30:28 +02:00
|
|
|
config = require('../../server/config'),
|
2017-12-11 22:47:46 +01:00
|
|
|
proxy = require('./proxy');
|
2017-04-04 17:07:35 +01:00
|
|
|
|
|
|
|
// Register an async handlebars helper for a given handlebars instance
|
|
|
|
function asyncHelperWrapper(hbs, name, fn) {
|
|
|
|
hbs.registerAsyncHelper(name, function returnAsync(context, options, cb) {
|
|
|
|
// Handle the case where we only get context and cb
|
|
|
|
if (!cb) {
|
|
|
|
cb = options;
|
|
|
|
options = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap the function passed in with a when.resolve so it can return either a promise or a value
|
2017-08-27 17:06:44 +02:00
|
|
|
Promise.resolve(fn.call(this, context, options)).then(function asyncHelperSuccess(result) {
|
2017-04-04 17:07:35 +01:00
|
|
|
cb(result);
|
2017-08-27 17:06:44 +02:00
|
|
|
}).catch(function asyncHelperError(err) {
|
2017-12-11 22:47:46 +01:00
|
|
|
var wrappedErr = err instanceof proxy.errors.GhostError ? err : new proxy.errors.IncorrectUsageError({
|
2017-08-27 17:06:44 +02:00
|
|
|
err: err,
|
2017-09-05 20:11:59 +02:00
|
|
|
context: 'registerAsyncThemeHelper: ' + name,
|
|
|
|
errorDetails: {
|
|
|
|
originalError: err
|
|
|
|
}
|
2017-08-27 17:06:44 +02:00
|
|
|
}),
|
|
|
|
result = config.get('env') === 'development' ? wrappedErr : '';
|
|
|
|
|
2017-12-11 22:47:46 +01:00
|
|
|
proxy.logging.error(wrappedErr);
|
2017-08-27 17:06:44 +02:00
|
|
|
|
|
|
|
cb(new hbs.SafeString(result));
|
2017-04-04 17:07:35 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a handlebars helper for themes
|
|
|
|
module.exports.registerThemeHelper = function registerThemeHelper(name, fn) {
|
|
|
|
hbs.registerHelper(name, fn);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Register an async handlebars helper for themes
|
|
|
|
module.exports.registerAsyncThemeHelper = function registerAsyncThemeHelper(name, fn) {
|
|
|
|
asyncHelperWrapper(hbs, name, fn);
|
|
|
|
};
|