Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
babel-plugin-async-try-catch
Advanced tools
A Babel plugin which wraps the body of async functions in a try/catch block
NOTICE: This plugin is deprecated and is no longer needed since the bug it works around has been fixed in regenerator.
A Babel plugin which wraps the body of async functions in a try/catch block
npm install babel-plugin-async-try-catch
$ babel --plugins async-try-catch script.js
$ cat before.js
function asyncError (error) {
console.error('error:', error);
}
async function printFile (filename) {
let contents = await fs.readFileAsync(filename, 'utf8');
console.log(contents);
}
$ babel --plugins async-try-catch --whitelist es7.asyncFunctions before.js
function asyncError (error) {
console.error('error:', error);
}
async function printFile (filename) {
try {
let contents = await fs.readFileAsync(filename, 'utf8');
console.log(contents);
} catch (error) {
asyncError.call(this, error);
}
}
This is a Babel plugin which wraps the body of async functions in a try/catch block.
If an exception is thrown, it is passed to a callback whose this
value is set
to the this
value inside the catch block. The callback name is currently hardwired to
asyncError
, but this will be configurable when Babel adds support for
plugin options.
If an async function's sole top-level statement is a try/catch, try/finally or try/catch/finally statement, it is not wrapped.
The ES7 async/await proposal makes asynchronous programming in JavaScript heavenly, but async functions have one major gotcha: they silently swallow exceptions raised within the body of the function. As a result, it is recommended to wrap the body of async functions inside a try/catch block:
Another, more insidious problem is that you have to be careful to wrap your code in try/catches, or else a promise might be rejected, in which case the error is silently swallowed. (!)
My advice is to ensure that your async functions are entirely surrounded by try/catches, at least at the top level
Nolan Lawson — Taming the asynchronous beast with ES7
This plugin automatically surrounds the body of async functions with a try/catch block, so you can take advantage of the sanity and simplicity of async/await without the boilerplate.
If you find yourself still manually writing try/catch blocks in order to perform custom error handling, it's worth remembering that the callback can be defined/overridden locally e.g. rather than writing:
async function printFile (filename) {
try {
let contents = await fs.readFileAsync(filename, 'utf8');
console.log(contents);
} catch (error) {
console.error(`error reading ${filename}:`, error.stack);
}
}
- you could write:
async function printFile (filename) {
var asyncError = error => console.error(`error reading ${filename}:`, error.stack);
let contents = await fs.readFileAsync(filename, 'utf8');
console.log(contents);
}
Note the use of var
(rather than let
or const
) to
hoist
the asyncError
declaration out of the try block so that it remains visible in the catch block.
It's also worth remembering that information about the call site at which an error occurred can easily be determined from an exception's stack trace.
0.0.9
Copyright © 2015 by chocolateboy
This module is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.
FAQs
A Babel plugin which wraps the body of async functions in a try/catch block
We found that babel-plugin-async-try-catch 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
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.