-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
events: migrate to internal/errors #15623
Conversation
ping @nodejs/tsc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a few nits. LGTM if CI is green.
doc/api/errors.md
Outdated
<a id="ERR_EVENTS_MAX_LISTENERS_TYPE"></a> | ||
### ERR_EVENTS_MAX_LISTENERS_TYPE | ||
|
||
Used when an attempt is made to set `require('events').defaultMaxListeners` to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to link this to the documentation of EventEmitter.defaultMaxListeners
lib/events.js
Outdated
throw new TypeError('"listener" argument must be a function'); | ||
if (typeof listener !== 'function') { | ||
const errors = lazyErrors(); | ||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener', 'Function'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/Function/function/
? Most of the ERR_INVALID_ARG_TYPE
s in the code base are using lowercased types.
lib/events.js
Outdated
if (typeof listener !== 'function') { | ||
const errors = lazyErrors(); | ||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener', | ||
'Function'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our docs mainly use Function
but it is going to print type Function
and this is somewhat inconsistent out of my perspective. I think we should stick to the lower cased version.
if (typeof listener !== 'function') | ||
throw new TypeError('"listener" argument must be a function'); | ||
if (typeof listener !== 'function') { | ||
const errors = lazyErrors(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know where the circular reference is coming from? If so - is there any possibility in untying that?
I personally also prefer using a simple if a lot over the function version. It is still a minor overhead of using the function while there is no faster check than a simple if to undefined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a circular reference that is the concern. The events
module is one of the very first modules loaded in core (because it is needed by process
) and as such needs to be loaded before anything else is loaded.
lib/events.js
Outdated
throw new TypeError('"defaultMaxListeners" must be a positive number'); | ||
if (typeof arg !== 'number' || arg < 0 || arg !== arg) { | ||
const errors = lazyErrors(); | ||
throw new errors.TypeError('ERR_EVENTS_MAX_LISTENERS_TYPE'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not really see the point for this new error type. I it extremely specific and the type name is somewhat confusing for me. I would expect it to describe having e.g. to many listeners. Using a more generic one that is similar to the old error message seems more appropriate to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The challenge is that none of the existing error codes quite fit this pattern (e.g. it's a setter and not an argument or an option).
doc/api/errors.md
Outdated
an invalid type. | ||
|
||
<a id="ERR_EVENTS_UNHANDLED_ERROR"></a> | ||
### ERR_EVENTS_UNHANDLED_ERROR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have to make this specific to the events? I would rather have this in case any unhandled error is thrown / emitted what ever.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really the only place that we raise such errors that I can find.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is correct for the current situation but for me it is more about trying to be open for future uses.
lib/internal/errors.js
Outdated
E('ERR_EVENTS_UNHANDLED_ERROR', | ||
(err) => { | ||
const msg = 'Unhandled "error" event.'; | ||
if (!err) return msg; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using if (err !== undefined)
would be better as there might be other falsy values out there that someone throws.
{ | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
type: TypeError, | ||
message: 'The "n" argument must be of type positive number' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a really confusing error message. I think we have a more appropriate error message for things like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The obsolete error code should be removed. LGTM otherwise (I do not feel that strongly about the more generic name even if I would still prefer it without "EVENTS").
doc/api/errors.md
Outdated
an invalid type. | ||
|
||
<a id="ERR_EVENTS_UNHANDLED_ERROR"></a> | ||
### ERR_EVENTS_UNHANDLED_ERROR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is correct for the current situation but for me it is more about trying to be open for future uses.
doc/api/errors.md
Outdated
@@ -644,6 +644,17 @@ according to the encoding provided. | |||
Used by the `util.TextDecoder()` API when the encoding provided is not one of | |||
the [WHATWG Supported Encodings][]. | |||
|
|||
<a id="ERR_EVENTS_MAX_LISTENERS_TYPE"></a> | |||
### ERR_EVENTS_MAX_LISTENERS_TYPE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type is not used anymore and should be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh right... heh, forgot about that ;-)
1288134
to
fd7b3cc
Compare
@BridgeAR .. updated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change looks good, but I think we need to add a test for the new error in test/parallel/test-internal-errors.js
if (errors === undefined) | ||
errors = require('internal/errors'); | ||
return errors; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to lazily load this? Is this the pattern used everywhere? Should we?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is because events.js is loaded before all the other modules in the dependency tree (because of bootstrapping process
. Lazy loading errors
prevents errors
from being loaded before events is loaded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would still rather inline this instead of using the function form but it should not be a blocker.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@BridgeAR are you ok with the change? Can we land? |
Would be better if we can specify the range with |
{ | ||
code: 'ERR_UNHANDLED_ERROR', | ||
type: Error, | ||
message: 'Unhandled error. (Accepts a string)' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The message looks somewhat weird here because of the dot before the brackets.
I think it would be fine to either remove the dot or to move it to the end of the string.
if (errors === undefined) | ||
errors = require('internal/errors'); | ||
return errors; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would still rather inline this instead of using the function form but it should not be a blocker.
@mcollina thanks for the ping |
fd7b3cc
to
006d963
Compare
PR-URL: #15623 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
Landed in e5ad545 |
PR-URL: nodejs/node#15623 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
Migrate events.js to internal errors
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
events,errors