-
Notifications
You must be signed in to change notification settings - Fork 190
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
better error handling #36
Conversation
timaschew
commented
Dec 7, 2015
- provide error handling for callback
- provide default error handling if no callback is passed
- provide error handling for callback - provide default error handling if no callback is passed
try { | ||
(callback || function(err) { | ||
if (!err) { | ||
console.log(err) |
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 really mean to only log here if !err
?
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.
Yes. lol no I mean err != null
but this was criticized by the linter, of course I mean !!err
I think if the caller is not interested in handling any successful callback, because it's the last action anyway or so, then he should be notified in the error case.
But you can also omit the logging in the binary here https://github.com/tschaub/gh-pages/blob/master/bin/gh-pages#L35
and do a successful log in this file.
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'm just confused why you would want to log undefined
or other falsy values.
To be clear, did you mean to write this:
if (err) {
console.log(err);
}
instead of this:
if (!err) {
console.log(err);
}
?
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.
Sorry, you're right, I edit my previous answer.
But if (err){}
didn't work, the error is falsy, so !!err
handles it the right way or err != null
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.
and err != null
was linted with this error: gh-pages/lib/index.js 63:13 error Use ‘===’ to compare with ‘null’ no-eq-null
which is really sad, because null check with !=
makes sense to check against undefined
and null
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.
haha !!
is not allowed too.
So either one of the rules needs to be disabled or the check should be
if (typeof err !== 'undefined' && err !== null) {}
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.
@timaschew you can just write this:
if (err) {
// do something
}
This behaves exactly the same as this:
if (!!err) {
// do something
}
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's true ^^
I tried this in the morning today, but seems that I was confused a little bit...