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

console: timeEnd() doesn't throw an error if no matching label #5901

Closed
wants to merge 1 commit into from
Closed

console: timeEnd() doesn't throw an error if no matching label #5901

wants to merge 1 commit into from

Conversation

ghaiklor
Copy link
Contributor

Pull Request check-list

  • Does make -j8 test (UNIX) or vcbuild test nosign (Windows) pass with
    this change (including linting)?
  • Is the commit message formatted according to [CONTRIBUTING.md][0]?
  • If this change fixes a bug (or a performance problem), is a regression
    test (or a benchmark) included?
  • Is a documentation update included (if this change modifies
    existing APIs, or introduces new ones)?

Affected core subsystem(s)

console

Description of change

Based on #3514 discussion, I've removed throw an error in timeEnd() method, if there is no matching label.

if (!stdout || typeof stdout.write !== 'function') {
throw new TypeError('Console expects a writable stream instance');
}
if (!(this instanceof Console)) return new Console(stdout, stderr);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please revert these style changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cjihrig What style guides do you using? Should I revert all the style changes or only this one?

Copy link
Contributor

Choose a reason for hiding this comment

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

We have an ESLint setup. But, in general, you should change the bare minimum in your PRs. Otherwise, it makes tools like git blame harder to use.

@ghaiklor
Copy link
Contributor Author

@cjihrig done

@mscdex mscdex added the console Issues and PRs related to the console subsystem. label Mar 25, 2016
@@ -8,7 +8,8 @@ assert.ok(process.stderr.writable);
assert.equal('number', typeof process.stdout.fd);
assert.equal('number', typeof process.stderr.fd);

assert.throws(function() {
assert.doesNotThrow(function() {
console.time('some label');
Copy link
Contributor

Choose a reason for hiding this comment

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

This is unnecessary.

@ghaiklor
Copy link
Contributor Author

@cjihrig got it, so we can ignore non-existing label.

throw new Error('No such label: ' + label);
}
const time = this._times.get(label);
if (!time) return;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you just replace the old throw with return;. It just needs to be a one line change.

@cjihrig
Copy link
Contributor

cjihrig commented Mar 25, 2016

I think so (see #3514 (comment)). What you had before was wrong. It would print something but it had the incorrect times.

@@ -8,10 +8,6 @@ assert.ok(process.stderr.writable);
assert.equal('number', typeof process.stdout.fd);
assert.equal('number', typeof process.stderr.fd);

assert.throws(function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't mean to delete the whole test. Just the one line that I commented on was unnecessary.

@ghaiklor
Copy link
Contributor Author

@cjihrig here is my code:

var console = require('console');

console.time('test');
console.timeEnd('no such label');
console.timeEnd('test');

Prints only for test label:

./node test.js
test: 0.652ms

The following code:

var console = require('console');
console.timeEnd('no such label');

prints nothing.

}

Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove this newly added line.

@cjihrig
Copy link
Contributor

cjihrig commented Mar 25, 2016

LGTM with one comment.

@ghaiklor
Copy link
Contributor Author

@cjihrig I see now. I must change only lines where it actually needs. Updated.

@cjihrig cjihrig added the semver-major PRs that contain breaking changes and should be released in the next major version. label Mar 25, 2016
@cjihrig
Copy link
Contributor

cjihrig commented Mar 25, 2016

@jasnell
Copy link
Member

jasnell commented Mar 26, 2016

LGTM

@thefourtheye
Copy link
Contributor

We are doing this so that we would be on par with browsers? It feels like what we are doing is correct. If there is a typo in the name or if the usage is wrong we explicitly throw and let the user know what is wrong.

@jasnell
Copy link
Member

jasnell commented Mar 26, 2016

How about a compromise? Don't throw but emit a warning using process.emitWarning.

@ghaiklor
Copy link
Contributor Author

In case of wrong label you get nothing in the console. That way you will know that you forgot to close console.time(). Looks as expected behaviour for me.

@jasnell
Copy link
Member

jasnell commented Apr 18, 2016

@thefourtheye ... do you still object to this or ...?

@thefourtheye
Copy link
Contributor

Yes, I am still not convinced that this change is better.

To think of it, emitting a warning, as you suggested, would be the right fit here. This is not a serious error, I agree, but there should be some feedback to the user.

if (!time) {
throw new Error('No such label: ' + label);
return;
Copy link
Member

@jasnell jasnell Apr 19, 2016

Choose a reason for hiding this comment

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

@ghaiklor ... would it be possible for you to add a ...

process.emitWarning(`No such label: ${label}`);

...before returning here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we include console.timeEnd somewhere in the message?

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 hurt... No such label '${label}' for console.timeEnd() perhaps? Keep in mind, also, that the warning includes a stack trace.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, then the message is just fine.

@ghaiklor
Copy link
Contributor Author

@jasnell @thefourtheye yeah, I will update branch in few hours.

@ghaiklor
Copy link
Contributor Author

Warning doesn't include a stack trace. So I updated message with @jasnell approach.

> console.timeEnd('no label');
> (node:51305) Warning: No such label 'no label' for console.timeEnd()

@jasnell
Copy link
Member

jasnell commented Apr 19, 2016

@ghaiklor. It does, it's just suppressed in the default output. If you use the --trace-warnings flag it will print the stack trace. And if you watch for warnings using process.on('warning', function(warn) { /** ... **/ });, the warn object will have a stack property that returns the stack trace.

@jasnell
Copy link
Member

jasnell commented Apr 19, 2016

one additional suggestion: in the test case, you can add a check to make sure the warning is issued appropriately... something like...

process.on('warning', common.mustCall((warning) => {
  assert(/No such label 'no label' for console.timeEnd\(\)/.test(warning.message));
}));

@jasnell
Copy link
Member

jasnell commented Apr 19, 2016

LGTM with a suggestion.

@ghaiklor
Copy link
Contributor Author

I've added check for warning event in the test case.

@thefourtheye
Copy link
Contributor

LGTM

@@ -1,5 +1,5 @@
'use strict';
require('../common');
var common = require('../common');
var assert = require('assert');
Copy link
Member

Choose a reason for hiding this comment

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

nit: these requires should use const

@jasnell
Copy link
Member

jasnell commented Apr 20, 2016

couple more nits after adding the warning check

assert.throws(function() {
assert.doesNotThrow(function() {
const message = /No such label 'no such label' for console\.timeEnd\(\)/;
const onWarning = common.mustCall(warning => assert(message.test(warning.message)));
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this pass linting?

Copy link
Contributor

Choose a reason for hiding this comment

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

It shouldn't. We added a rule where parens are explicitly required I though

@cjihrig
Copy link
Contributor

cjihrig commented Apr 20, 2016

Yea, still LGTM once the comments are addressed.

@ghaiklor
Copy link
Contributor Author

@cjihrig updated and fixed conflicts with master branch.

@cjihrig
Copy link
Contributor

cjihrig commented Apr 21, 2016

It would probably be simpler to use assert.strictEqual() instead of a regular expression in your test, but I don't think it needs to hold this us. Could you squash your changes down to a single commit?

@ghaiklor
Copy link
Contributor Author

@cjihrig yes, I can, but I'm not sure if I can push them with force flag. As I understand, I need to do:

git rebase -i master

Squash all commits in one and then

git push --force origin fix/3514

@cjihrig
Copy link
Contributor

cjihrig commented Apr 21, 2016

It's fine to force push your own branch. No one else is using it.

@ghaiklor
Copy link
Contributor Author

@cjihrig done

const message = /No such label 'no such label' for console\.timeEnd\(\)/;
const onWarning = (warning) => assert(message.test(warning.message));
process.once('warning', common.mustCall(onWarning));

Copy link
Member

Choose a reason for hiding this comment

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

this could likely be rewritten and simplified to just:

process.once('warning', common.mustCall((warning) => {
  assert(/no such label/.test(warning.message);
});

There's likely very little need to test the entire warning message. You just want to verify that the warning is being issued.

@jasnell
Copy link
Member

jasnell commented Apr 21, 2016

Just one last nit while I was looking it back over :-) Looking good!

When timeEnd() provided with label that doesn't exists
it emits warning in the console, so developer get know about it.
@ghaiklor
Copy link
Contributor Author

@jasnell done.

BTW, why do I need squash commits by myself if it can be done when merging into master (after rebase)?

@jasnell
Copy link
Member

jasnell commented Apr 21, 2016

@ghaiklor ... while it's possible for the person landing the PR to squash everything, it's often easier for those of us who do the landing to have the author of the PR squash them down (typically because it saves time). I personally don't mind squashing commits when I land things.

@jasnell
Copy link
Member

jasnell commented Apr 21, 2016

New CI: https://ci.nodejs.org/job/node-test-pull-request/2359/
LGTM if CI is green

@ghaiklor
Copy link
Contributor Author

@jasnell I see, thanks for explanation 😃
CI is green, my first contribution ^^

@jasnell
Copy link
Member

jasnell commented Apr 21, 2016

Awesome! Thank you for the contribution and for the patience! @cjihrig ... does this LGTY?

@cjihrig
Copy link
Contributor

cjihrig commented Apr 21, 2016

:shipit: (yes, LGTM)

@jasnell
Copy link
Member

jasnell commented Apr 21, 2016

Awesome. Given that this is a semver-major, let's give time for one more round of last call comments from @nodejs/ctc. If there are no objections by Monday I'll get this landed.

@jasnell jasnell added this to the 6.0.0 milestone Apr 22, 2016
jasnell pushed a commit that referenced this pull request Apr 25, 2016
When timeEnd() provided with label that doesn't exists
it emits warning in the console, so developer get know about it.

PR-URL: #5901
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
@jasnell
Copy link
Member

jasnell commented Apr 25, 2016

Landed in 1c84579

@jasnell jasnell closed this Apr 25, 2016
joelostrowski pushed a commit to joelostrowski/node that referenced this pull request Apr 25, 2016
When timeEnd() provided with label that doesn't exists
it emits warning in the console, so developer get know about it.

PR-URL: nodejs#5901
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
@ghaiklor ghaiklor deleted the fix/3514 branch April 25, 2016 17:37
jasnell pushed a commit that referenced this pull request Apr 26, 2016
When timeEnd() provided with label that doesn't exists
it emits warning in the console, so developer get know about it.

PR-URL: #5901
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
console Issues and PRs related to the console subsystem. semver-major PRs that contain breaking changes and should be released in the next major version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants