Skip to content
This repository was archived by the owner on Feb 4, 2018. It is now read-only.

Commit b9403b8

Browse files
authored
Merge pull request #77 from bem-sdk/yeti-or.improve-err-msg
fix(error): error message more handy
2 parents 68a59c1 + 23fb979 commit b9403b8

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

index.js

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

33
const util = require('util');
44

5+
const ExtendableError = require('es6-error');
56
const stringifyEntity = require('@bem/naming').stringify;
67

78
/**
@@ -17,6 +18,19 @@ const TYPES = {
1718
ELEM_MOD: 'elemMod'
1819
};
1920

21+
class NotValidEntityError extends ExtendableError {
22+
/**
23+
* @param {object} obj — not valid object
24+
* @param {str} [reason] — reason why object is not valid
25+
*/
26+
constructor(obj, reason) {
27+
const str = util.inspect(obj, { depth: 1 });
28+
const message = `The object \`${str}\` is not valid BEM entity`;
29+
30+
super(reason ? `${message}: ${reason}` : message);
31+
}
32+
}
33+
2034
module.exports = class BemEntityName {
2135
/**
2236
* @param {object} obj — representation of entity name.
@@ -31,7 +45,7 @@ module.exports = class BemEntityName {
3145
*/
3246
constructor(obj) {
3347
if (!obj.block) {
34-
throw new Error('This is not valid BEM entity: the field `block` is undefined.');
48+
throw new NotValidEntityError(obj, 'the field `block` is undefined');
3549
}
3650

3751
const data = this._data = { block: obj.block };
@@ -48,7 +62,7 @@ module.exports = class BemEntityName {
4862
val: hasModVal ? modObj && modObj.val || obj.modVal : true
4963
};
5064
} else if (modObj || hasModVal) {
51-
throw new Error('This is not valid BEM entity: the field `mod.name` is undefined.');
65+
throw new NotValidEntityError(obj, 'the field `mod.name` is undefined');
5266
}
5367

5468
this.__isBemEntityName__ = true;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"node": ">= 4.0"
3030
},
3131
"dependencies": {
32-
"@bem/naming": "2.0.0-5"
32+
"@bem/naming": "2.0.0-5",
33+
"es6-error": "4.0.2"
3334
},
3435
"devDependencies": {
3536
"ava": "^0.18.0",

test/construct.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ test('should create object and normalize boolean modifier', t => {
2929
test('should throw error for if entity object is not valid', t => {
3030
t.throws(
3131
() => new BemEntityName({ elem: 'elem' }),
32-
'This is not valid BEM entity: the field `block` is undefined.'
32+
"The object `{ elem: 'elem' }` is not valid BEM entity: the field `block` is undefined"
3333
);
3434
});
3535

3636
test('should throw error for if mod object is empty', t => {
3737
t.throws(
3838
() => new BemEntityName({ block: 'block', mod: {} }),
39-
'This is not valid BEM entity: the field `mod.name` is undefined.'
39+
"The object `{ block: 'block', mod: {} }` is not valid BEM entity: the field `mod.name` is undefined"
4040
);
4141
});
4242

4343
test('should throw error for if mod name is undefined', t => {
4444
t.throws(
4545
() => new BemEntityName({ block: 'block', mod: { val: 'val' } }),
46-
'This is not valid BEM entity: the field `mod.name` is undefined.'
46+
"The object `{ block: 'block', mod: { val: 'val' } }` is not valid BEM entity: the field `mod.name` is undefined"
4747
);
4848
});
File renamed without changes.

0 commit comments

Comments
 (0)