Friday, 17 March 2017

Is there an error-control operator (@) in nodejs like php

Okay, so I know the first words out of any nodejs developer is going to be "DON'T DO IT".

My curiosity got the best of me today, but I couldn't find any documentation on it. I'm not sure if the lack of documentation means it doesn't exist, or if it's not documented because it's not desirable.

In PHP you use the handy (though usually misused) '@' operator in front of functions so that it doesn't report errors.

While writing some test today for a nodejs project, each test needs to delete a specific file ./.tmp/email.html by simply doing fs.unlinkSync('./.tmp/email.html', 'utf-8'). There's a couple tests that don't produce the file so my 'after' hook for those tests fail. The obvious fix is to test if the file actually exists before trying to delete it.

That's when my curiosity got me. In the days of PHP (okay, I still do quite a bit of Wordpress development), I would simply add a little @ symbol in front to shut-up the complaints of the file not existing as I really don't care in this particular situation whether the file exists or not. Then I would use a global error_handler to catch any of those errors just for information purposes.

So the question is: is there an error-control operator in nodejs like there is in PHP. I'm not asking whether it's a good idea or not.



via Senica Gonzalez

No comments:

Post a Comment