Skip to content

Commit 3d5123e

Browse files
Merge pull request #29 from cossssmin/refactor/errors
Refactor error messages
2 parents 853e1d5 + 5ddee42 commit 3d5123e

3 files changed

Lines changed: 16 additions & 5 deletions

File tree

src/find-path.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function searchInFolders(tag, fileNameFromTag, options) {
4444
const componentPath = search(options.root, options.folders, fileNameFromTag, options.fileExtension);
4545

4646
if (!componentPath) {
47-
throw new Error(`[components] For the tag ${tag} was not found any template in defined root path ${options.folders.join(', ')}`);
47+
throw new Error(`[components] <${tag}> could not find ${fileNameFromTag} in the defined root paths (${options.folders.join(', ')})`);
4848
}
4949

5050
return componentPath;
@@ -63,7 +63,7 @@ function searchInNamespaces(tag, [namespace, fileNameFromTag], options) {
6363
const namespaceOption = options.namespaces.find(n => n.name === namespace.replace(options.tagPrefix, ''));
6464

6565
if (!namespaceOption) {
66-
throw new Error(`[components] Unknown component namespace ${namespace}.`);
66+
throw new Error(`[components] Unknown component namespace: ${namespace}.`);
6767
}
6868

6969
let componentPath;
@@ -84,7 +84,7 @@ function searchInNamespaces(tag, [namespace, fileNameFromTag], options) {
8484
}
8585

8686
if (!componentPath && options.strict) {
87-
throw new Error(`[components] For the tag ${tag} was not found any template in the defined namespace's base path ${namespaceOption.root}.`);
87+
throw new Error(`[components] <${tag}> could not find ${fileNameFromTag} in the defined namespace base path ${namespaceOption.root}`);
8888
}
8989

9090
return componentPath;

src/process-stacks.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {match} = require('posthtml/lib/api');
44
const {render} = require('posthtml-render');
5+
const get = require('lodash/get');
56

67
/**
78
* Process <push> tag
@@ -13,8 +14,12 @@ const {render} = require('posthtml-render');
1314
*/
1415
function processPushes(tree, content, push) {
1516
match.call(tree, {tag: push}, pushNode => {
17+
if (get(pushNode, 'attrs.name') === '') {
18+
throw new Error(`[components] <${push}> tag requires a value for the "name" attribute.`);
19+
}
20+
1621
if (!pushNode.attrs || !pushNode.attrs.name) {
17-
throw new Error(`[components] Push <${push}> tag must have an attribute "name".`);
22+
throw new Error(`[components] <${push}> tag requires a "name" attribute.`);
1823
}
1924

2025
if (!content[pushNode.attrs.name]) {

test/test-errors.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,14 @@ test('Must fail when component is not found in defined namespace with strict mod
5959
await t.throwsAsync(async () => posthtml([plugin({root: './test/templates', strict: true, namespaces: [{name: 'empty-namespace', root: './test/templates/empty-namespace'}]})]).process(actual).then(result => clean(result.html)));
6060
});
6161

62-
test('Must fail when push tag missing name', async t => {
62+
test('Must fail when push tag missing name attribute', async t => {
6363
const actual = `<div><push></push></div>`;
6464

6565
await t.throwsAsync(async () => posthtml([plugin({root: './test/templates', strict: true})]).process(actual).then(result => clean(result.html)));
6666
});
67+
68+
test('Must fail when push tag missing name attribute value', async t => {
69+
const actual = `<div><push name></push></div>`;
70+
71+
await t.throwsAsync(async () => posthtml([plugin({root: './test/templates', strict: true})]).process(actual).then(result => clean(result.html)));
72+
});

0 commit comments

Comments
 (0)