Skip to content

Commit

Permalink
chore(SlashCommandBuilder): improve message when missing required params
Browse files Browse the repository at this point in the history
  • Loading branch information
wwselleck committed Apr 28, 2023
1 parent e0e3753 commit 7fbef78
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Expand Up @@ -116,12 +116,12 @@ describe('Slash Commands', () => {

test('GIVEN missing required parameters THEN throw error', () => {
expect(() => SlashCommandAssertions.validateRequiredParameters(null, 'My name is missing', [])).toThrowError(
'Expected a string primitive',
'Required parameter "name" is missing',
);

expect(() =>
SlashCommandAssertions.validateRequiredParameters('my-description-is-missing', null, []),
).toThrowError('Expected a string primitive');
).toThrowError('Required parameter "description" is missing');
});
});

Expand Down
17 changes: 15 additions & 2 deletions packages/builders/src/interactions/slashCommands/Assertions.ts
Expand Up @@ -5,6 +5,11 @@ import type { ToAPIApplicationCommandOptions } from './SlashCommandBuilder.js';
import type { SlashCommandSubcommandBuilder, SlashCommandSubcommandGroupBuilder } from './SlashCommandSubcommands.js';
import type { ApplicationCommandOptionBase } from './mixins/ApplicationCommandOptionBase.js';

export class MissingRequiredParameterError extends Error {
public constructor(missingParameter: string) {
super(`Required parameter "${missingParameter}" is missing`);
}
}
const namePredicate = s.string
.lengthGreaterThanOrEqual(1)
.lengthLessThanOrEqual(32)
Expand Down Expand Up @@ -35,14 +40,22 @@ export function validateMaxOptionsLength(options: unknown): asserts options is T
}

export function validateRequiredParameters(
name: string,
description: string,
name: string | undefined,
description: string | undefined,
options: ToAPIApplicationCommandOptions[],
) {
// Assert name matches all conditions
if (!name) {
throw new MissingRequiredParameterError('name');
}

validateName(name);

// Assert description conditions
if (!description) {
throw new MissingRequiredParameterError('description');
}

validateDescription(description);

// Assert options conditions
Expand Down

0 comments on commit 7fbef78

Please sign in to comment.