Ghost/core/server/utils/labs.js
Hannah Wolfe 852155075f 🐛 Added 409 DisabledFeatureError for labs features (#8890)
fixes #8889

- This is a user error, not a system error
- Downgrading to a 4xx status code means it doesn't appear in logs where it shouldn't
- We didn't have a suitable error available so I added DisabledFeatureError with 409 status
- Ref: https://stackoverflow.com/questions/36874263/expected-http-status-code-for-an-action-on-a-disabled-resource
- Also tweaked the error message slightly as it didn't read clearly to me
2017-08-15 10:50:36 +02:00

47 lines
1.4 KiB
JavaScript

var settingsCache = require('../settings/cache'),
_ = require('lodash'),
Promise = require('bluebird'),
SafeString = require('../themes/engine').SafeString,
errors = require('../errors'),
logging = require('../logging'),
i18n = require('../i18n'),
labs = module.exports = {};
labs.isSet = function isSet(flag) {
var labsConfig = settingsCache.get('labs');
return labsConfig && labsConfig[flag] && labsConfig[flag] === true;
};
labs.enabledHelper = function enabledHelper(options, callback) {
var errDetails, errString;
if (labs.isSet(options.flagKey) === true) {
// helper is active, use the callback
return callback();
}
// Else, the helper is not active and we need to handle this as an error
errDetails = {
message: i18n.t('warnings.helpers.helperNotAvailable', {helperName: options.helperName}),
context: i18n.t('warnings.helpers.flagMustBeEnabled', {
helperName: options.helperName,
flagName: options.flagName
}),
help: i18n.t('warnings.helpers.seeLink', {url: options.helpUrl})
};
logging.error(new errors.DisabledFeatureError(errDetails));
errString = new SafeString(
'<script>console.error("' + _.values(errDetails).join(' ') + '");</script>'
);
if (options.async) {
return Promise.resolve(function asyncError() {
return errString;
});
}
return errString;
};