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

error: switch to existing ERR_INVALID_ARG_VALUE #97

Merged
merged 2 commits into from
Apr 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ class ERR_INVALID_ARG_TYPE extends TypeError {
}
}

class ERR_INVALID_SHORT_OPTION extends TypeError {
constructor(longOption, shortOption) {
super(`options.${longOption}.short must be a single character, got '${shortOption}'`);
this.code = 'ERR_INVALID_SHORT_OPTION';
class ERR_INVALID_ARG_VALUE extends TypeError {
constructor(arg1, arg2, expected) {
super(`The property ${arg1} ${expected}. Received '${arg2}'`);
this.code = 'ERR_INVALID_ARG_VALUE';
}
}

module.exports = {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_SHORT_OPTION
ERR_INVALID_ARG_VALUE
}
};
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const {

const {
codes: {
ERR_INVALID_SHORT_OPTION,
ERR_INVALID_ARG_VALUE,
},
} = require('./errors');

Expand Down Expand Up @@ -117,7 +117,11 @@ const parseArgs = ({
const shortOption = optionConfig.short;
validateString(shortOption, `options.${longOption}.short`);
if (shortOption.length !== 1) {
throw new ERR_INVALID_SHORT_OPTION(longOption, shortOption);
throw new ERR_INVALID_ARG_VALUE(
`options.${longOption}.short`,
shortOption,
'must be a single character'
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ test('invalid short option length', (t) => {
const passedOptions = { foo: { short: 'fo', type: 'boolean' } };

t.throws(function() { parseArgs({ args: passedArgs, options: passedOptions }); }, {
code: 'ERR_INVALID_SHORT_OPTION'
code: 'ERR_INVALID_ARG_VALUE'
});

t.end();
Expand Down