diff --git a/lib/plugins/tag/post_link.js b/lib/plugins/tag/post_link.js index f27cec0c4e..d70ada3726 100644 --- a/lib/plugins/tag/post_link.js +++ b/lib/plugins/tag/post_link.js @@ -12,9 +12,10 @@ const { postFindOneFactory } = require('./'); */ module.exports = ctx => { return function postLinkTag(args) { - const error = `Post not found: ${args.join(' ') || 'Invalid post_link'}`; const slug = args.shift(); - if (!slug) return error; + if (!slug) { + throw new Error(`Post not found: "${slug}" doesn't exist for {% post_link %}`); + } let escape = args[args.length - 1]; if (escape === 'true' || escape === 'false') { @@ -24,7 +25,9 @@ module.exports = ctx => { } const post = postFindOneFactory(ctx)({ slug }); - if (!post) return error; + if (!post) { + throw new Error(`Post not found: post_link ${slug}.`); + } let title = args.length ? args.join(' ') : post.title; const attrTitle = escapeHTML(title); diff --git a/test/scripts/tags/post_link.js b/test/scripts/tags/post_link.js index e0a9d3c6a5..473edd5f41 100644 --- a/test/scripts/tags/post_link.js +++ b/test/scripts/tags/post_link.js @@ -57,11 +57,11 @@ describe('post_link', () => { .should.eql('This is a Bold "statement"'); }); - it('no slug', () => { - postLink([]).should.eql('Post not found: Invalid post_link'); + it('should throw if no slug', () => { + should.throw(() => postLink([]), Error, /Post not found: "undefined" doesn't exist for \{% post_link %\}/); }); - it('post not found', () => { - postLink(['bar']).should.eql('Post not found: bar'); + it('should throw if post not found', () => { + should.throw(() => postLink(['bar']), Error, /Post not found: post_link bar\./); }); });