8000 feature #42090 [Notifier] [Slack] Include additional errors to slack … · symfony/symfony@b5d4b33 · GitHub
[go: up one dir, main page]

Skip to content

Commit b5d4b33

Browse files
committed
feature #42090 [Notifier] [Slack] Include additional errors to slack notifier error message (norkunas)
This PR was merged into the 5.4 branch. Discussion ---------- [Notifier] [Slack] Include additional errors to slack notifier error message | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | N/A Sometimes it is hard to debug why notification was rejected, so I am proposing to include error descriptions to the exception message. Commits ------- 18c1498 Include additional errors to slack notifier error message
2 parents 01613b1 + 18c1498 commit b5d4b33

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Symfony/Component/Notifier/Bridge/Slack/SlackTransport.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ protected function doSend(MessageInterface $message): SentMessage
9595

9696
$result = $response->toArray(false);
9797
if (!$result['ok']) {
98-
throw new TransportException(sprintf('Unable to post the Slack message: "%s".', $result['error']), $response);
98+
$errors = isset($result['errors']) ? ' ('.implode('|', $result['errors']).')' : '';
99+
100+
throw new TransportException(sprintf('Unable to post the Slack message: "%s"%s.', $result['error'], $errors), $response);
99101
}
100102

101103
$sentMessage = new SentMessage($message, (string) $this);

src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackTransportTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,32 @@ public function testSendIncludesContentTypeWithCharset()
239239

240240
$transport->send(new ChatMessage('testMessage'));
241241
}
242+
243+
public function testSendWithErrorsIncluded()
244+
{
245+
$response = $this->createMock(ResponseInterface::class);
246+
247+
$response->expects($this->exactly(2))
248+
->method('getStatusCode')
249+
->willReturn(200);
250+
251+
$response->expects($this->once())
252+
->method('getContent')
253+
->willReturn(json_encode([
254+
'ok' => false,
255+
'error' => 'invalid_blocks',
256+
'errors' => ['no more than 50 items allowed [json-pointer:/blocks]'],
257+
]));
258+
259+
$client = new MockHttpClient(function () use ($response): ResponseInterface {
260+
return $response;
261+
});
262+
263+
$transport = $this->createTransport($client, 'testChannel');
264+
265+
$this->expectException(TransportException::class);
266+
$this->expectExceptionMessage('Unable to post the Slack message: "invalid_blocks" (no more than 50 items allowed [json-pointer:/blocks]).');
267+
268+
$transport->send(new ChatMessage('testMessage'));
269+
}
242270
}

0 commit comments

Comments
 (0)
0