Skip to content

Commit

Permalink
refactor!: switch to existing ERR_INVALID_ARG_VALUE (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe authored Apr 10, 2022
1 parent 4903e5e commit 084a23f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
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

0 comments on commit 084a23f

Please sign in to comment.