Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
callback-wrappers
Advanced tools
wrappers for async callbacks that implement common error handling scenarios
Function wrappers for async callbacks that implement common, simple error handling scenarios.
npm install callback-wrappers
Most async methods in the node world expect a callback with an (error, data)
signature.
In programming scenarios where complex error handling is impossible or unneccessary (for
example you can simply log the error and exit the process) this can generate a lot
of repetitive, boilerplate, error-handling code that can obscure your real logic, e.g.
asyncFunction1({ ... }, function (error, data) {
if (error) {
console.error(error);
process.exit(1);
} else {
// some real logic here
asyncFunction2({ ... }, function (error, data) {
if (error) {
console.error(error);
process.exit(2);
} else {
// some more real logic here
}
});
}
});
This module provides a bunch of wrappers that take a function with just (data)
signature
and produce a function with the (error, data)
signature and the boiler plate logic in place.
For example the exitIfError
wrapper has the exact logic shown above, allowing for us to
collapse that example down to
var exitIfError = require("callback-wrappers").exitIfError;
asyncFunction1({ ... }, exitIfError(1, function (data) {
// some real logic here
asyncFunction2({ ... }, exitIfError(2, function (data) {
// some more real logic here
});
});
There's also a nextIfError
wrapper that takes a function with a (data, next)
signature (where next
is a callback of the (error, ...)
variety). This simply
passes error
to next
(and, unlike the other methods, does not log).
If messing with built-in objects' prototypes doesn't skeeve you out, you can use
the Function()
export to get Function.prototype decorated with all of the wrappers
and our example can look like
require("callback-wrappers").Function();
asyncFunction1({ ... }, function (data) {
// some real logic here
asyncFunction2({ ... }, function (data) {
// some more real logic here
}.exitIfError(2));
}.exitIfError(1));
The wrappers all follow a naming convention of actionIfError
, where
action is one of log
, abort
, exit
, throw
or next
. For brevity these
can be referenced by the initials l
, a
, x
, t
and n
, followed by ie
(for "If Error"). Note that in all cases the error is logged, and in no case,
including logIfError
, will the wrapped function be called if the
error
parameter isn't empty.
Thanks go to the nodejs group for comments and suggestions.
MIT
FAQs
wrappers for async callbacks that implement common error handling scenarios
The npm package callback-wrappers receives a total of 0 weekly downloads. As such, callback-wrappers popularity was classified as not popular.
We found that callback-wrappers demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.