Made callback truly optional; made ERR() return the err #3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@Pita,
I understand if you're not interested in this pull request, as it is a breaking change for any code that relied on the throwing behavior of
ERR()
. However, it makesERR()
more flexible by allowing it to do just one thing: augment the stack of the passed error object.For instance, instead of requiring that it be called like this:
it can also be called like this:
which is arguably clearer (the call to
callback
is explicit and the decoration of theerr
object is implicit).On a more important note, it allows
callback
to be referenced just once in both error and non-error scenarios, like this:And it allows callback to be called in non-standard ways:
Ok, that was a contrived example, but the point is that it brings more flexibility by allowing the calling code to take responsibility for calling the callback how and when it sees fit and allowing
ERR()
to do just one thing: augment the error object's stack.This pull request also still supports the previous behavior: if a
callback
is passed as the second argument, it will still be called byERR()
as before. So the only code that will break is code that does not pass a callback and expectsERR()
to throw an exception in the case of an error.If exception-throwing behavior is desired, it can still be done -- and in an (arguably) clearer way -- by calling
ERR()
like this:I updated the relevant tests and documentation in this pull request.