-
Notifications
You must be signed in to change notification settings - Fork 54
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
Add an error code to the unkown option
error
#8
Conversation
So it's easier to catch it
I'm 👎 on this since IMO However, including the parser state in the error message as properties wouldn't be the worst. |
Another approach would be to export custom error types. In arg: // Utility function to create custom errors
const createError = name => class extends Error {
constructor(message) {
super(message);
this.name = name;
}
};
// Custom error types
const UnknownOrUnexpectedOptionError = createError('UnknownOrUnexpectedOptionError');
const MissingOptionArgumentError = createError('MissingOptionArgumentError');
// …
// And thrown this way
throw new UnknownOrUnexpectedOptionError(`Unknown or unexpected option: ${originalArgName}`); It would then make it easy to know what error is being thrown: const arg = require('arg');
const { UnknownOrUnexpectedOptionError } = arg;
try {
const args = arg({ /* … */ });
} catch (err) {
if (err instanceof UnknownOrUnexpectedOptionError) {
// handle the error gracefully
}
} |
@matheuss can you:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Add an error code to the `unkown option` error So it's easier to catch it * Change error code * Document * Fix markdown
Before:
After: