Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add .catch to command declaration #113

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ItsMeBrianD
Copy link

πŸ”— Linked issue

Resolves #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)
  • ✨ 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

  • 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
Copy link
Author

@pi0 any idea when this can get looked at?

@pi0
Copy link
Member

pi0 commented Mar 1, 2024

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

@ItsMeBrianD
Copy link
Author

@pi0 any updates here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for custom error handling
2 participants