2020-03-30 23:23:02 +03:00
|
|
|
const Promise = require('bluebird');
|
2020-04-08 21:02:31 +03:00
|
|
|
const hbs = require('../engine');
|
|
|
|
const config = require('../../../../server/config');
|
|
|
|
const {errors, logging} = require('../../../../server/lib/common');
|
2017-04-04 19:07:35 +03: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 18:06:44 +03:00
|
|
|
Promise.resolve(fn.call(this, context, options)).then(function asyncHelperSuccess(result) {
|
2017-04-04 19:07:35 +03:00
|
|
|
cb(result);
|
2017-08-27 18:06:44 +03:00
|
|
|
}).catch(function asyncHelperError(err) {
|
2020-03-30 23:23:02 +03:00
|
|
|
var wrappedErr = err instanceof errors.GhostError ? err : new errors.IncorrectUsageError({
|
2017-08-27 18:06:44 +03:00
|
|
|
err: err,
|
2017-09-05 21:11:59 +03:00
|
|
|
context: 'registerAsyncThemeHelper: ' + name,
|
|
|
|
errorDetails: {
|
|
|
|
originalError: err
|
|
|
|
}
|
2017-08-27 18:06:44 +03:00
|
|
|
}),
|
|
|
|
result = config.get('env') === 'development' ? wrappedErr : '';
|
|
|
|
|
2020-03-30 23:23:02 +03:00
|
|
|
logging.error(wrappedErr);
|
2017-08-27 18:06:44 +03:00
|
|
|
|
|
|
|
cb(new hbs.SafeString(result));
|
2017-04-04 19:07:35 +03: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);
|
|
|
|
};
|