Skip to content

Commit

Permalink
[Notifier] Introduce LengthException
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark committed Dec 16, 2020
1 parent 75323bd commit 437cad7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Notifier\Bridge\Discord;

use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\LengthException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\ChatMessage;
Expand Down Expand Up @@ -67,7 +67,7 @@ protected function doSend(MessageInterface $message): SentMessage
$content = $message->getSubject();

if (\strlen($content) > 2000) {
throw new LogicException('The subject length of a Discord message must not exceed 2000 characters.');
throw new LengthException('The subject length of a Discord message must not exceed 2000 characters.');
}

$endpoint = sprintf('https://%s/api/webhooks/%s/%s', $this->getEndpoint(), $this->webhookId, $this->token);
Expand Down
Expand Up @@ -14,7 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransport;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\LengthException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\ChatMessage;
Expand Down Expand Up @@ -55,7 +55,7 @@ public function testSendChatMessageWithMoreThan2000CharsThrowsLogicException()
{
$transport = new DiscordTransport('testToken', 'testChannel', $this->createMock(HttpClientInterface::class));

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

$transport->send(new ChatMessage(str_repeat('d', 2001)));
Expand Down
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Notifier\Bridge\Slack\Block;

use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\LengthException;

/**
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
Expand All @@ -26,7 +26,7 @@ final class SlackHeaderBlock extends AbstractSlackBlock
public function __construct(string $text)
{
if (\strlen($text) > self::TEXT_LIMIT) {
throw new LogicException(sprintf('Maximum length for the text is %d characters.', self::TEXT_LIMIT));
throw new LengthException(sprintf('Maximum length for the text is %d characters.', self::TEXT_LIMIT));
}

$this->options = [
Expand All @@ -41,7 +41,7 @@ public function __construct(string $text)
public function id(string $id): self
{
if (\strlen($id) > self::ID_LIMIT) {
throw new LogicException(sprintf('Maximum length for the block id is %d characters.', self::ID_LIMIT));
throw new LengthException(sprintf('Maximum length for the block id is %d characters.', self::ID_LIMIT));
}

$this->options['block_id'] = $id;
Expand Down
Expand Up @@ -13,7 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackHeaderBlock;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\LengthException;

final class SlackHeaderBlockTest extends TestCase
{
Expand All @@ -34,15 +34,15 @@ public function testCanBeInstantiated(): void

public function testThrowsWhenTextExceedsCharacterLimit(): void
{
$this->expectException(LogicException::class);
$this->expectException(LengthException::class);
$this->expectExceptionMessage('Maximum length for the text is 150 characters.');

new SlackHeaderBlock(str_repeat('h', 151));
}

public function testThrowsWhenBlockIdExceedsCharacterLimit(): void
{
$this->expectException(LogicException::class);
$this->expectException(LengthException::class);
$this->expectExceptionMessage('Maximum length for the block id is 255 characters.');

$header = new SlackHeaderBlock('header');
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Notifier/Exception/LengthException.php
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Exception;

/**
* @author Oskar Stark <oskarstark@googlemail.com>
*
* @experimental in 5.3
*/
class LengthException extends LogicException
{
}

0 comments on commit 437cad7

Please sign in to comment.