Skip to content

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?

@ItsMeBrianD
Copy link
Author

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

@ItsMeBrianD
Copy link
Author

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

@pi0 pi0 mentioned this pull request Mar 8, 2025
@pi0
Copy link
Member

pi0 commented Mar 8, 2025

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

@pi0 pi0 closed this Mar 8, 2025
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