citty icon indicating copy to clipboard operation
citty copied to clipboard

feat: add .catch to command declaration

Open ItsMeBrianD opened this issue 2 years ago โ€ข 3 comments

๐Ÿ”— 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

ItsMeBrianD avatar Jan 11 '24 18:01 ItsMeBrianD

@pi0 any idea when this can get looked at?

ItsMeBrianD avatar Mar 01 '24 16:03 ItsMeBrianD

Thanks for the PR. Honestly im not sure about catch to be best naming idea and i need to think about that

pi0 avatar Mar 01 '24 23:03 pi0

@pi0 any updates here?

ItsMeBrianD avatar May 09 '24 03:05 ItsMeBrianD

@pi0 just want to check and see if there is anything I can do to get this across the line

ItsMeBrianD avatar Oct 15 '24 20:10 ItsMeBrianD

@pi0 would really like to get this merged so we can use mainline

ItsMeBrianD avatar Dec 10 '24 17:12 ItsMeBrianD

Sorry for all delay, moving to #185 since couldn't push to your fork branch.

pi0 avatar Mar 08 '25 21:03 pi0