-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
doc: Fix message type in Assert docs #16427
Conversation
The Assert Docs currently state that type `{any}` is accepted for the `message` arg in all of the assert methods. It should be `{string|undefined}`.
I think {any} is fine:
|
Thank you for responding so quickly. This is what I am experiencing. I am unable to access properties on the message of type
It appears you can input {any}, but you are returned {string|undefined}. Perhaps this PR should be to add expected return types to the docs. In order to access the properties of my object message I had to JSON.stringify and then JSON.parse which defeats the purpose. try {
assert.equal(1, 2, JSON.stringify({ status: 404, detail: 'Not Found' }));
} catch (e) {
const parsed = JSON.parse(e.message);
ctx.throw(parsed.status, parsed.detail);
} |
@cottrellio - thanks for the quick revert. As long as a toString() is present, the data is meaningfully transported to the assertion site, but definitely not iterable, as it is now a flat string. const assert = require('assert')
const util = require('util')
const obj = {status: 404, detail: 'Not found',
toString: () => { return "'status': 404, 'detail': 'not found'"}}
try {
assert.equal(1, 2, obj)
} catch(e) {
console.log(obj === e)
console.log(util.inspect(obj, {showHidden: true, depth: null}))
console.log(util.inspect(e))
} Assuming that the purpose of the assertion is to communicate the violation of a pre-condition or an un-expected program state, the Adding a caveat to the doc to this effect (that user defined objects passed to the assert will be stringified based on toString()) looks to be more reasonable to me, but let us wait for more views from other collaborators. |
@gireeshpunathil Again, thank you for your response. I agree with your rational. Thank you for clarifying. |
It is basically like doing: new Error({ status: 404, detail: 'Not Found' }) I think it makes sense. |
sort of stuck here. @lpinca - can you please clarify your last comment? do you endorse the changes as it is, or propose alternatives? |
@gireeshpunathil I agree with what you wrote in #16427 (comment). Keeping |
ping @nodejs/collaborators to get some concurrence on the proposal, and based on it ping @cottrellio to amend the doc accordingly |
|
There is no return type. const assert = require('assert');
var foo = assert.ok(true, 'message value');
console.log(foo); // undefined
try {
foo = assert.fail('message here');
} catch (e) {
}
console.log(foo); // undefined |
I'm not sure it defeats the purpose. Assertions should terminate your program. Using them for programmatic logic seems to suggest that something else would be better and/or that we're missing functionality in Node.js that needs to be added. |
@cottrellio Thanks for the pull request! It seems to me like this can be closed, but if you disagree, leave a comment and we'll re-open. Hope you have everything you need! |
doc: fix message type in assert docs
The Assert Docs currently state that type
{any}
is accepted for themessage
arg in all of the assert methods. It should be{string|undefined}
.Checklist
Affected core subsystem(s)