[nodejs] Enhancement: assert.ifError(value)

135 views
Skip to first unread message

Mikeal Rogers

unread,
May 19, 2010, 6:24:10 PM5/19/10
to nod...@googlegroups.com
I added a method to the assert module that is really useful when
testing callbacks.

fs.readFile('asdf.txt', function (error) { assert.ifError(error) });

Patch includes unit tests and docs.

-Mikeal

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

Jonas Pfenniger (zimbatm)

unread,
May 22, 2010, 9:06:18 AM5/22/10
to nod...@googlegroups.com
Should't it be called assert.noError instead ?

The name is what you're asserting, and assertion error is thrown if
the assertion turns false.

Felix Geisendörfer

unread,
May 22, 2010, 12:20:29 PM5/22/10
to nodejs
> Should't it be called assert.noError instead ?

+1

I actually don't think we should even support this method, at least I
don't need it.

--fg

On May 22, 9:06 am, "Jonas Pfenniger (zimbatm)" <jo...@pfenniger.name>
wrote:
> Should't it be called assert.noError instead ?
>
> The name is what you're asserting, and assertion error is thrown if
> the assertion turns false.
>
>
>
>
>
> On Thu, May 20, 2010 at 12:24 AM, Mikeal Rogers <mikeal.rog...@gmail.com> wrote:
> > I added a method to the assert module that is really useful when
> > testing callbacks.
>
> > fs.readFile('asdf.txt', function (error) { assert.ifError(error) });
>
> > Patch includes unit tests and docs.
>
> > -Mikeal
>
> > --
> > You received this message because you are subscribed to the Google Groups "nodejs" group.
> > To post to this group, send email to nod...@googlegroups.com.
> > To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/nodejs?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/nodejs?hl=en.

Jan Schütze

unread,
May 22, 2010, 12:32:15 PM5/22/10
to nod...@googlegroups.com
Since we have:

assert.ok(value, message)
// Tests if value is a true value, it is equivalent to
assert.equal(true, value, message);

I would prefer a:

assert.notOk(value, message)
// Tests if value is a false value, it is equivalent to
assert.equal(false, value, message);

- Jan

2010/5/22 Felix Geisendörfer <fe...@debuggable.com>:
--

http://dracoblue.net

Mikeal Rogers

unread,
May 22, 2010, 1:31:05 PM5/22/10
to nod...@googlegroups.com
The difference is that you want to throw the error that is passed and
not an AssertError.

assert.notOk is a not a drop in replacement for.

if (error) {throw error;}

I don't care about the naming, I suck at names.

-Mikeal

2010/5/22 Jan Schütze <Ja...@dracoblue.de>:

Kris Kowal

unread,
May 22, 2010, 1:37:38 PM5/22/10
to nod...@googlegroups.com
On Sat, May 22, 2010 at 10:31 AM, Mikeal Rogers <mikeal...@gmail.com> wrote:
> The difference is that you want to throw the error that is passed and
> not an AssertError.
>
> assert.notOk is a not a drop in replacement for.
>
> if (error) {throw error;}
>
> I don't care about the naming, I suck at names.

Ah, at least this has been an interesting exercise into what people
infer from the names "ifError" and "noError", since there's no link to
the patch in your original email. It might be more clear that the
error is to be thrown if it is defined with the name "throwIf".

Kris Kowal

Felix Geisendörfer

unread,
May 22, 2010, 1:49:51 PM5/22/10
to nodejs
I don't know guys:

if (err) throw err;
assert.noError(err);

You are typing 1 more character and this is better because ...?

--fg

On May 22, 1:37 pm, Kris Kowal <kris.ko...@cixar.com> wrote:

Jonas Pfenniger (zimbatm)

unread,
May 22, 2010, 2:48:07 PM5/22/10
to nod...@googlegroups.com
On Sat, May 22, 2010 at 7:31 PM, Mikeal Rogers <mikeal...@gmail.com> wrote:
> The difference is that you want to throw the error that is passed and
> not an AssertError.

Then it doesn't have it's place in the "assert" library.

The noError method could make sense if it checks for a value that is
not an instanceof Error, but then it must at least throw an
AssertError (with the other error embedded if you want).

Mikeal Rogers

unread,
May 22, 2010, 3:20:10 PM5/22/10
to nod...@googlegroups.com
Checking for instance of error isn't in keeping with the standard
callback API. If *anything* is passed as the first argument that is
truthy then an error has occurred. While node is good about passing an
Error object a lot of third party modules aren't.

I don't agree that it "doesn't belong" in the assert library because
the behavior differs slighlty and throws a different error type.
That's sounds like some silly notion of purity that I just don't
understand.

If it's useful for testing then it's a good addition, if it's not
useful then it doesn't belong. Felix makes some good points that it
might not be that useful, I never use that single line if style so I
didn't realize how much less code it could be.

-Mikeal

Felix Geisendörfer

unread,
May 22, 2010, 4:38:50 PM5/22/10
to nodejs
> I never use that single line if style so I
> didn't realize how much less code it could be.

I don't use it in my regular code either, but I actually do use it for
stuff like this in my test cases.

--fg

On May 22, 3:20 pm, Mikeal Rogers <mikeal.rog...@gmail.com> wrote:
> Checking for instance of error isn't in keeping with the standard
> callback API. If *anything* is passed as the first argument that is
> truthy then an error has occurred. While node is good about passing an
> Error object a lot of third party modules aren't.
>
> I don't agree that it "doesn't belong" in the assert library because
> the behavior differs slighlty and throws a different error type.
> That's sounds like some silly notion of purity that I just don't
> understand.
>
> If it's useful for testing then it's a good addition, if it's not
> useful then it doesn't belong. Felix makes some good points that it
> might not be that useful, I never use that single line if style so I
> didn't realize how much less code it could be.
>
> -Mikeal
>
> On Sat, May 22, 2010 at 11:48 AM, Jonas Pfenniger (zimbatm)
>
>
>
>
>
> <jo...@pfenniger.name> wrote:
> > On Sat, May 22, 2010 at 7:31 PM, Mikeal Rogers <mikeal.rog...@gmail.com> wrote:
> >> The difference is that you want to throw the error that is passed and
> >> not an AssertError.
>
> > Then it doesn't have it's place in the "assert" library.
>
> > The noError method could make sense if it checks for a value that is
> > not an instanceof Error, but then it must at least throw an
> > AssertError (with the other error embedded if you want).
>
> > --
> > You received this message because you are subscribed to the Google Groups "nodejs" group.
> > To post to this group, send email to nod...@googlegroups.com.
> > To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/nodejs?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/nodejs?hl=en.
Reply all
Reply to author
Forward
0 new messages