Throw parser specific error

This commit is contained in:
Erik 2022-07-29 20:23:45 +03:00
parent e332907fa2
commit cc03b96442
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB
3 changed files with 13 additions and 10 deletions

View File

@ -1,4 +1,4 @@
export { Parser } from './src/Parser'; export { Parser, ParseError } from './src/Parser';
export { Command } from './src/classes/Command'; export { Command } from './src/classes/Command';
export { CommandOption } from './src/classes/CommandOption'; export { CommandOption } from './src/classes/CommandOption';

View File

@ -1,6 +1,6 @@
{ {
"name": "commandparser", "name": "commandparser",
"version": "1.0.15", "version": "1.0.16",
"description": "Parser meant to parse commands and their options for discord bots", "description": "Parser meant to parse commands and their options for discord bots",
"main": "build/index.js", "main": "build/index.js",
"author": "Navy.gif", "author": "Navy.gif",

View File

@ -1,3 +1,4 @@
// eslint-disable-next-line max-classes-per-file
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import CommandOption from './classes/CommandOption'; import CommandOption from './classes/CommandOption';
@ -21,6 +22,8 @@ type ParserOptions = {
resolver: IResolver<unknown, unknown, unknown, unknown, unknown> resolver: IResolver<unknown, unknown, unknown, unknown, unknown>
} }
class ParseError extends Error {}
const flagReg = /(?:^| )(?<flag>(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu; const flagReg = /(?:^| )(?<flag>(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu;
class Parser extends EventEmitter { class Parser extends EventEmitter {
@ -89,7 +92,7 @@ class Parser extends EventEmitter {
parseResult.subcommand = subcommand?.name || null; parseResult.subcommand = subcommand?.name || null;
parseResult.subcommandGroup = group?.name || null; parseResult.subcommandGroup = group?.name || null;
if (!subcommand) throw new Error(`Expecting subcommand, one of ${subcommands.map((s) => s.name)}`); //return { showUsage: true, verbose: true }; if (!subcommand) throw new ParseError(`Expecting subcommand, one of ${subcommands.map((s) => s.name)}`); //return { showUsage: true, verbose: true };
this.debug(`Got ${subcommand.name}`); this.debug(`Got ${subcommand.name}`);
} }
@ -126,7 +129,7 @@ class Parser extends EventEmitter {
aliased = f.valueAsAlias && f.choices.some((c) => c === _flag); aliased = f.valueAsAlias && f.choices.some((c) => c === _flag);
return f.name === _flag || aliased; return f.name === _flag || aliased;
}); });
if (!flag) throw new Error(`Unrecognised flag: ${_flag}`); if (!flag) throw new ParseError(`Unrecognised flag: ${_flag}`);
this.debug(`Matched flag: ${flag.name} with ${_flag}`); this.debug(`Matched flag: ${flag.name} with ${_flag}`);
params.splice(index, 1, ''); params.splice(index, 1, '');
@ -147,9 +150,9 @@ class Parser extends EventEmitter {
const result = await flag.parse(guild); const result = await flag.parse(guild);
if (result.error) { if (result.error) {
if (flag.choices.length) { if (flag.choices.length) {
throw new Error(`Invalid choice for ${flag.name}, Valid choices are ${flag.choices.join(', ')}.`); throw new ParseError(`Invalid choice for ${flag.name}, Valid choices are ${flag.choices.join(', ')}.`);
} }
throw new Error(`Failed to parse value for ${flag.name}, expected value type: ${flag.type}`); //return { option: flag, ...result.removed }; throw new ParseError(`Failed to parse value for ${flag.name}, expected value type: ${flag.type}`); //return { option: flag, ...result.removed };
} }
this.debug(`Cleaning up params after ${flag.name}`); this.debug(`Cleaning up params after ${flag.name}`);
for (const r of result.removed) params.splice(params.indexOf(r), 1); for (const r of result.removed) params.splice(params.indexOf(r), 1);
@ -231,15 +234,15 @@ class Parser extends EventEmitter {
if (!arg.choices.some((choice) => { if (!arg.choices.some((choice) => {
if (typeof arg.value === 'string' && typeof choice === 'string') return arg.value.toLowerCase() === choice.toLowerCase(); if (typeof arg.value === 'string' && typeof choice === 'string') return arg.value.toLowerCase() === choice.toLowerCase();
return arg.value === choice; return arg.value === choice;
})) throw new Error(`Invalid choice: ${arg.name} value must be one of ${arg.choices.join(', ')}`); //return { error: true, index: 'O_COMMANDHANDLER_INVALID_CHOICE', params: { option: arg.name, value: arg.value, choices: arg.choices.map((c) => c).join('`, `') } }; })) throw new ParseError(`Invalid choice: ${arg.name} value must be one of ${arg.choices.join(', ')}`); //return { error: true, index: 'O_COMMANDHANDLER_INVALID_CHOICE', params: { option: arg.name, value: arg.value, choices: arg.choices.map((c) => c).join('`, `') } };
} }
this.debug(`Making sure required options were given.`); this.debug(`Making sure required options were given.`);
for (const req of activeCommand.options.filter((opt) => opt.required)) for (const req of activeCommand.options.filter((opt) => opt.required))
if (!args[req.name]) if (!args[req.name])
throw new Error(`${req.name} is a required option`); throw new ParseError(`${req.name} is a required option`);
if (strings.length) throw new Error(`Unrecognised option(s): "${strings.join('", "')}"`);//return { error: true, index: 'O_COMMANDHANDLER_UNRECOGNISED_OPTIONS', params: { opts: strings.join('`, `') } }; if (strings.length) throw new ParseError(`Unrecognised option(s): "${strings.join('", "')}"`);//return { error: true, index: 'O_COMMANDHANDLER_UNRECOGNISED_OPTIONS', params: { opts: strings.join('`, `') } };
return parseResult; return parseResult;
@ -251,4 +254,4 @@ class Parser extends EventEmitter {
} }
export { Parser }; export { Parser, ParseError };