Skip to content

[Notifier] [Discord] Fix exception message + test #39444

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

Merged
merged 1 commit into from
Dec 14, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function doSend(MessageInterface $message): SentMessage
$content = $message->getSubject();

if (\strlen($content) > 2000) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe time to move this to a private const?
also, we should consider using mb_strlen maybe? that'd require first checking if 2000 UTF-8 chars are fine
last but not least, see https://support.discord.com/hc/en-us/community/posts/360031093812:

29/10/2020 edit: it's been kinda fixed. Now, if you type over the char limit discord will send the message as a TXT file. Not ideal but it's something.

this is worth a small investigation if anyone is up to trying with an actual Discord server

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #39492

throw new LogicException(sprintf('The subject length of "%s" transport must be less than 2000 characters.', __CLASS__));
throw new LogicException('The subject length of a Discord message must not exceed 2000 characters.');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if this should be an InvalidArgumentException 🧐

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LengthException could be a good fit.
That's for 5.x anyway I suppose :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix itself is for 5.2, but we can use LengthException in 5.x, what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR welcome now that it's merged.

}

$endpoint = sprintf('https://%s/api/webhooks/%s/%s', $this->getEndpoint(), $this->webhookId, $this->token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public function testSendNonChatMessageThrows()
$transport->send($this->createMock(MessageInterface::class));
}

public function testSendChatMessageWithMoreThan2000CharsThrowsLogicException()
{
$transport = new DiscordTransport('testToken', 'testChannel', $this->createMock(HttpClientInterface::class));

$this->expectException(LogicException::class);
$this->expectExceptionMessage('The subject length of a Discord message must not exceed 2000 characters.');

$transport->send(new ChatMessage(str_repeat('d', 2001)));
}

public function testSendWithErrorResponseThrows()
{
$this->expectException(TransportException::class);
Expand Down