Skip to content

Commit

Permalink
feat(noon): send an error message if the guild cannot be found in the…
Browse files Browse the repository at this point in the history
… message

Closes #1896.
  • Loading branch information
C0ZEN committed Dec 1, 2022
1 parent 194ab13 commit 04b7466
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { LoggerService } from '../../../../../../../../logger/services/logger.se
import { DiscordChannelService } from '../../../../../../../channels/services/discord-channel.service';
import { IDiscordMessageResponse } from '../../../../../../interfaces/discord-message-response';
import { IAnyDiscordMessage } from '../../../../../../types/any-discord-message';
import { DiscordMessageErrorService } from '../../../../../helpers/discord-message-error.service';
import { DiscordMessageCommandFeatureNoonFlagEnum } from '../enums/discord-message-command-feature-noon-flag.enum';
import { Message } from 'discord.js';
import { createMock } from 'ts-auto-mock';
Expand All @@ -22,11 +23,13 @@ describe(`DiscordMessageCommandFeatureNoonStatus`, (): void => {
let loggerService: LoggerService;
let firebaseGuildsStoreService: FirebaseGuildsStoreService;
let discordChannelService: DiscordChannelService;
let discordMessageErrorService: DiscordMessageErrorService;

beforeEach((): void => {
loggerService = LoggerService.getInstance();
firebaseGuildsStoreService = FirebaseGuildsStoreService.getInstance();
discordChannelService = DiscordChannelService.getInstance();
discordMessageErrorService = DiscordMessageErrorService.getInstance();
});

describe(`execute()`, (): void => {
Expand Down Expand Up @@ -261,6 +264,7 @@ describe(`DiscordMessageCommandFeatureNoonStatus`, (): void => {

let loggerServiceErrorSpy: jest.SpyInstance;
let firebaseGuildsStoreQueryGetEntitySpy: jest.SpyInstance;
let discordMessageErrorServiceHandleErrorSpy: jest.SpyInstance;

beforeEach((): void => {
service = new DiscordMessageCommandFeatureNoonStatus();
Expand All @@ -271,6 +275,9 @@ describe(`DiscordMessageCommandFeatureNoonStatus`, (): void => {
firebaseGuildsStoreQueryGetEntitySpy = jest
.spyOn(firebaseGuildsStoreService, `getEntity`)
.mockReturnValue(undefined);
discordMessageErrorServiceHandleErrorSpy = jest
.spyOn(discordMessageErrorService, `handleError`)
.mockImplementation();
});

describe(`when the given Discord message guild is null`, (): void => {
Expand All @@ -281,19 +288,19 @@ describe(`DiscordMessageCommandFeatureNoonStatus`, (): void => {
});
});

it(`should log about the empty guild`, async (): Promise<void> => {
it(`should log and send an error message both in the channel and the Sonia guild errors channel`, async (): Promise<void> => {
expect.assertions(3);

await expect(service.isEnabled(anyDiscordMessage)).rejects.toThrow(
new Error(`Could not get the guild from the message`)
);

expect(loggerServiceErrorSpy).toHaveBeenCalledTimes(1);
expect(loggerServiceErrorSpy).toHaveBeenCalledWith({
context: `DiscordMessageCommandFeatureNoonStatus`,
hasExtendedContext: true,
message: `context-[dummy-id] text-could not get the guild from the message`,
} as ILoggerLog);
expect(discordMessageErrorServiceHandleErrorSpy).toHaveBeenCalledTimes(1);
expect(discordMessageErrorServiceHandleErrorSpy).toHaveBeenCalledWith(
new Error(`Could not get the guild from the message`),
anyDiscordMessage,
`could not get the guild from the message`
);
});

it(`should throw an error`, async (): Promise<void> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DiscordChannelService } from '../../../../../../../channels/services/di
import { DiscordCommandFlagActionValueless } from '../../../../../../classes/commands/flags/discord-command-flag-action-valueless';
import { IDiscordMessageResponse } from '../../../../../../interfaces/discord-message-response';
import { IAnyDiscordMessage } from '../../../../../../types/any-discord-message';
import { DiscordMessageErrorService } from '../../../../../helpers/discord-message-error.service';
import { Snowflake } from 'discord.js';
import _ from 'lodash';

Expand All @@ -35,7 +36,7 @@ export class DiscordMessageCommandFeatureNoonStatus<T extends string> implements

public isEnabled(anyDiscordMessage: IAnyDiscordMessage): Promise<boolean | undefined> {
if (_.isNil(anyDiscordMessage.guild)) {
return this._getNoGuildMessageError(anyDiscordMessage.id);
return this._getNoGuildMessageError(anyDiscordMessage);
}

const firebaseGuild: IFirebaseGuild | undefined = FirebaseGuildsStoreService.getInstance().getEntity(
Expand Down Expand Up @@ -107,17 +108,16 @@ export class DiscordMessageCommandFeatureNoonStatus<T extends string> implements
return Promise.reject(new Error(`Could not find the guild ${guildId} in Firebase`));
}

private _getNoGuildMessageError(discordMessageId: Snowflake): Promise<never> {
LoggerService.getInstance().error({
context: this._serviceName,
hasExtendedContext: true,
message: LoggerService.getInstance().getSnowflakeContext(
discordMessageId,
`could not get the guild from the message`
),
});
private _getNoGuildMessageError(anyDiscordMessage: IAnyDiscordMessage): Promise<never> {
const error: Error = new Error(`Could not get the guild from the message`);

DiscordMessageErrorService.getInstance().handleError(
error,
anyDiscordMessage,
`could not get the guild from the message`
);

return Promise.reject(new Error(`Could not get the guild from the message`));
return Promise.reject(error);
}

private _logExecuteAction(discordMessageId: Snowflake): void {
Expand Down

0 comments on commit 04b7466

Please sign in to comment.