Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try/catch for 4.x #299

Merged
merged 1 commit into from
Aug 3, 2016
Merged

Try/catch for 4.x #299

merged 1 commit into from
Aug 3, 2016

Conversation

miripiruni
Copy link
Contributor

@miripiruni miripiruni commented Jul 1, 2016

See #298

Original task: https://nda.ya.ru/3RiZp2

@miripiruni
Copy link
Contributor Author

Benchmark results in web4 https://nda.ya.ru/3RiZii

@miripiruni
Copy link
Contributor Author

PR: https://nda.ya.ru/3RiZik

@@ -947,7 +947,7 @@ describe('BEMHTML compiler/Runtime', function() {
});
});

it('should throw error with one apply', function() {
xit('should throw error with one apply', function() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only question is: what shall I do with this cases? I think, as long as we introduce try/catch for block rendering it not valid anymore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since it depend on this.options.debug there is should be two ways of tests: with throwing and without

@miripiruni
Copy link
Contributor Author

Review needed. cc @veged @zxqfox

@miripiruni miripiruni changed the title [WIP] Trycatch for 4.x Trycatch for 4.x Jul 11, 2016
@miripiruni miripiruni changed the title Trycatch for 4.x Try/catch for 4.x Jul 11, 2016
JSON.stringify(context.elemMods)
].join(' ') +
'\n' +
e +

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.stack?

@miripiruni
Copy link
Contributor Author

Updated.

@@ -375,7 +375,8 @@ BEMHTML.prototype.runOne = function runOne(json) {
ent.init(block, elem);
}

var res = ent.run(context);
var res = this.tryRun(context, ent);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FOR Minor: if debug !== true ent.run
FOR Maj: this.tryRun

@miripiruni
Copy link
Contributor Author

Updated. cc @tadatuta @veged

I introduced production flag. If it’s value true bem-xjst will render all templates. If there is an error bem-xjst will log it via console.error.

$ cat stand.js
var bem = require('./');
var template = bem.compile(function() {
  block('page').attrs()(function() {
    var undef = applyNext();
    return undef.test = 'foo';
  });
}, { production: true });
var html = template.apply({ block: 'wrap', content: { block: 'page' } });
console.log(html);


$ node stand.js
BEMXJST ERROR: cannot render block page, elem undefined, mods {}, elemMods {} [TypeError: Cannot set property 'test' of undefined]
<div class="wrap"></div>

This way allows us to maintain backward capability. Also developers do not miss errors, since by default the program will stop its work. We can release it as minor.

@miripiruni
Copy link
Contributor Author

miripiruni commented Jul 15, 2016

Notice that there is no documentation in v4.x when I port this feature to v5 and higher I also add documentation.

@miripiruni
Copy link
Contributor Author

@veged @tadatuta review needed. Thank you!

var res;

try {
res = ent.run(context);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can return right here without unnecessary res variable

@veged
Copy link
Member

veged commented Jul 22, 2016

LGTM, except one comment

try {
res = ent.run(context);
} catch (e) {
console.error('BEMXJST ERROR: cannot render ' +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be more informative to show whole context:

console.error('BEMXJST ERROR: cannot render ' + JSON.stringify(context, null, 4), e);

also it's possible to get rid of res:

try {
    return ent.run(context);
} catch (e) {
    console.error(/* ... */);
}

return '';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tadatuta context contains circular structure and many information useless for debugging in this case.

'elem ' + context.elem,
'mods ' + JSON.stringify(context.mods),
'elemMods ' + JSON.stringify(context.elemMods)
].join(', '), e);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.stack || e, please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@St-Lukas e.stack is useless:

93:trycatch miripiruni$ node stand.js
BEMXJST ERROR: cannot render block page, elem undefined, mods {}, elemMods {} TypeError: Cannot set property 'test' of undefined
at ContextChild. (evalmachine.:1863:19)
at Match.tryCatch (evalmachine.:1240:15)
at Match.exec (evalmachine.:1290:16)
at Entity.defaultBody (evalmachine.:266:26)
at Entity.run (evalmachine.:251:15)
at BEMHTML.tryRun (evalmachine.:681:16)
at BEMHTML.runOne (evalmachine.:663:10)
at BEMHTML._run (evalmachine.:531:16)
at BEMHTML.renderContent (evalmachine.:970:18)
at BEMHTML.renderClose (evalmachine.:831:19)

@miripiruni
Copy link
Contributor Author

JFYI: Pulse test results: https://nda.ya.ru/3RiZii

@miripiruni
Copy link
Contributor Author

I think we can merge it. And port to 5.x, 6.x, 7.x.

@veged
Copy link
Member

veged commented Aug 3, 2016

yes

@miripiruni miripiruni merged commit 8b3369f into v4.x Aug 3, 2016
@miripiruni miripiruni deleted the trycatch-for-4.x branch August 4, 2016 13:07
@miripiruni miripiruni added this to the 4.4.0 milestone Sep 12, 2016
@miripiruni
Copy link
Contributor Author

Also in v5.2.0

@miripiruni
Copy link
Contributor Author

Also in v6.7.0

@miripiruni
Copy link
Contributor Author

Also in v7.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants