Skip to content

Commit

Permalink
🍺 allow passing custom error handling functions in catch block as opt…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
vivek12345 committed Feb 1, 2019
1 parent 4fc1059 commit 142349b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,41 @@ async function completeApplicationFlow() {
jscodeshift -t transforms/async-await-with-try-catch.js <path>
```

If you want to replace the default `console.log(e)` with your custom function call for example `error.handleError(e)`,
you can pass that as an option `--catchBlock`

```sh
jscodeshift -t transforms/async-await-with-try-catch.js <path> --catchBlock=error.handleError
```

This will transform your code to:-

```javascript
async function completeApplicationFlow() {
// wait for get session status api to check the status
let response;
try {
response = await getSessionStatusApi();
} catch(e) {
error.handleError(e)
}
// wait for getting next set of questions api
try {
response = await getNextQuestionsApi();
} catch(e) {
error.handleError(e)
}
// finally submit application
try {
response = await submitApplication();
} catch(e) {
error.handleError(e)
}
}

```


### Recast Options

Options to [recast](https://github.com/benjamn/recast)'s printer can be provided
Expand Down
31 changes: 19 additions & 12 deletions transforms/async-await-with-try-catch.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
// Press ctrl+space for code completion

export default function transformer(file, api) {
export default function transformer(file, api, options) {
const j = api.jscodeshift;
const root = j(file.source);

const ExpressionTypes = ['BinaryExpression', 'LogicalExpression', 'NewExpression', 'ObjectExpression'];
const DisAllowedFunctionExpressionTypes = ['ArrowFunctionExpression', 'FunctionExpression', 'ObjectExpression'];

function getCatchBlockExpression() {
if(options.catchBlock) {
const [obj, property] = options.catchBlock.split('.');
if(property) {
return j.callExpression(j.memberExpression(j.identifier(obj), j.identifier(property)), [
j.identifier('e')
]);
} else {
return j.callExpression(j.identifier(obj), [
j.identifier('e')
]);
}
}
return j.callExpression(j.memberExpression(j.identifier('console'), j.identifier('log')), [j.identifier('e')]);
}
function isAlreadyInsideTryBlock(path) {
return j(path).closest(j.TryStatement).length;
}
Expand All @@ -13,15 +28,7 @@ export default function transformer(file, api) {
j(path).replaceWith(
j.tryStatement(
j.blockStatement([type]),
j.catchClause(
j.identifier('e'),
null,
j.blockStatement([
j.expressionStatement(
j.callExpression(j.memberExpression(j.identifier('console'), j.identifier('log')), [j.identifier('e')])
)
])
)
j.catchClause(j.identifier('e'), null, j.blockStatement([j.expressionStatement(getCatchBlockExpression())]))
)
);
}
Expand Down

0 comments on commit 142349b

Please sign in to comment.