feat: add .catch to command declaration
๐ Linked issue
Resolves unjs/citty#112
โ Type of change
- [ ] ๐ Documentation (updates to the documentation, readme, or JSdoc annotations)
- [ ] ๐ Bug fix (a non-breaking change that fixes an issue)
- [ ] ๐ Enhancement (improving an existing functionality like performance)
- [x] โจ New feature (a non-breaking change that adds functionality)
- [ ] ๐งน Chore (updates to the build process or auxiliary tools and libraries)
- [ ] โ ๏ธ Breaking change (fix or feature that would cause existing functionality to change)
๐ Description
This adds an additional function .catch to a command definition. It works similarly to setup and cleanup, but the error does not bubble all the way up if caught.
This allows for custom error handling that may differ from the default (e.g. changing the console output of a CLI when an uncaught exception occurs anywhere in the app).
Example usage of catch:
const main = defineCommand({
meta: {
name: 'my-cli',
description: 'my cli command'
},
/**
* @param {import("citty").CommandContext} ctx
* @param {Error} e
*/
catch(ctx, e) {
console.log("Error occured!", e.message) // -> Error occured! meep
process.exit(1);
},
run() {
throw new Error("meep")
}
});
runMain(main);
๐ Checklist
- [x] I have linked an issue or discussion.
- ~~[ ] I have updated the documentation accordingly.~~ There doesn't seem to be a place in the documentation to update, but I did write tests with full coverage
@pi0 any idea when this can get looked at?
Thanks for the PR. Honestly im not sure about catch to be best naming idea and i need to think about that
@pi0 any updates here?
@pi0 just want to check and see if there is anything I can do to get this across the line
@pi0 would really like to get this merged so we can use mainline
Sorry for all delay, moving to #185 since couldn't push to your fork branch.