🐛 Downgraded errors to warnings for img_url

refs #8703

- Instead of throwing errors, throw warnings for incorrect usage of the img_url helper
- Differentiate between no attribute passed, and attribute evaluating to undefined
This commit is contained in:
Hannah Wolfe 2017-08-27 19:06:53 +02:00 committed by Katharina Irrgang
parent c49dba12a0
commit 688d8c9051
3 changed files with 46 additions and 33 deletions

View File

@ -1,39 +1,39 @@
// Usage:
// `{{img_url}}` - does not work, argument is required
// `{{img_url feature_image}}`
// `{{img_url profile_image absolute="true"}}`
// Note:
// `{{img_url}}` - does not work, argument is required
//
// Returns the URL for the current object scope i.e. If inside a post scope will return image permalink
// `absolute` flag outputs absolute URL, else URL is relative.
var proxy = require('./proxy'),
errors = require('../errors'),
logging = require('../logging'),
i18n = require('../i18n'),
url = proxy.url;
module.exports = function imgUrl(attr, options) {
var absolute;
// CASE: if you pass e.g. cover_image, but it is not set, then attr is null!
// in this case we don't throw an error
if (!options) {
attr = undefined;
options = attr;
}
absolute = options && options.hash && options.hash.absolute;
if (attr === undefined) {
throw new errors.IncorrectUsageError({
message: i18n.t('warnings.helpers.img_url.attrIsRequired')
});
}
// CASE: property is not set in the model e.g. cover_image
if (attr === null) {
// CASE: if no attribute is passed, e.g. `{{img_url}}` we show a warning
if (arguments.length < 2) {
logging.warn(i18n.t('warnings.helpers.img_url.attrIsRequired'));
return;
}
return url.urlFor('image', {image: attr}, absolute);
var absolute = options && options.hash && options.hash.absolute;
// CASE: if attribute is passed, but it is undefined, then the attribute was
// an unknown value, e.g. {{img_url feature_img}} and we also show a warning
if (attr === undefined) {
logging.warn(i18n.t('warnings.helpers.img_url.attrIsRequired'));
return;
}
if (attr) {
return url.urlFor('image', {image: attr}, absolute);
} else {
// CASE: if you pass e.g. cover_image, but it is not set, then attr is null!
// in this case we don't show a warning
return;
}
};

View File

@ -521,7 +521,8 @@
"valuesMustBeDefined": "All values must be defined for empty, singular and plural"
},
"img_url": {
"attrIsRequired": "Attribute is required e.g. \\{\\{img_url feature_image\\}\\}"
"attrIsRequired": "Attribute is required e.g. \\{\\{img_url feature_image\\}\\}",
"attrIsUnknown": "Attribute passed to \\{\\{img_url\\}\\} is unknown"
},
"template": {
"templateNotFound": "Template {name} not found."

View File

@ -4,15 +4,21 @@ var should = require('should'), // jshint ignore:line
// Stuff we are testing
helpers = require('../../../server/helpers'),
errors = require('../../../server/errors'),
logging = require('../../../server/logging'),
sandbox = sinon.sandbox.create();
describe('{{image}} helper', function () {
var logWarnStub;
before(function () {
configUtils.set({url: 'http://localhost:82832/'});
});
beforeEach(function () {
logWarnStub = sandbox.stub(logging, 'warn');
});
afterEach(function () {
sandbox.restore();
});
@ -25,33 +31,39 @@ describe('{{image}} helper', function () {
var rendered = helpers.img_url('/content/images/image-relative-url.png', {});
should.exist(rendered);
rendered.should.equal('/content/images/image-relative-url.png');
logWarnStub.called.should.be.false();
});
it('should output absolute url of image if the option is present ', function () {
var rendered = helpers.img_url('/content/images/image-relative-url.png', {hash: {absolute: 'true'}});
should.exist(rendered);
rendered.should.equal('http://localhost:82832/content/images/image-relative-url.png');
logWarnStub.called.should.be.false();
});
it('should output author url', function () {
var rendered = helpers.img_url('/content/images/author-image-relative-url.png', {});
should.exist(rendered);
rendered.should.equal('/content/images/author-image-relative-url.png');
logWarnStub.called.should.be.false();
});
it('should have no output if there is no image ', function (done) {
try {
helpers.img_url(undefined, {hash: {absolute: 'true'}});
done(new Error('we expect an error from img_url'));
} catch (err) {
(err instanceof errors.IncorrectUsageError).should.eql(true);
done();
}
it('should have no output if the image attributeis not provided (with warning)', function () {
var rendered = helpers.img_url({hash: {absolute: 'true'}});
should.not.exist(rendered);
logWarnStub.calledOnce.should.be.true();
});
it('should have no output if there is no image ', function () {
it('should have no output if the image attribute evaluates to undefined (with warning)', function () {
var rendered = helpers.img_url(undefined, {hash: {absolute: 'true'}});
should.not.exist(rendered);
logWarnStub.calledOnce.should.be.true();
});
it('should have no output if the image attribute evaluates to null (no waring)', function () {
var rendered = helpers.img_url(null, {hash: {absolute: 'true'}});
should.not.exist(rendered);
logWarnStub.calledOnce.should.be.false();
});
describe('with sub-directory', function () {