Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Notifier] Make sure Http TransportException is not leaking #42182

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
Jul 19, 2021
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 @@ -18,6 +18,7 @@
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -77,17 +78,23 @@ protected function doSend(MessageInterface $message): SentMessage
'json' => array_filter($options),
]);

if (204 !== $response->getStatusCode()) {
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Discord server.', $response, 0, $e);
Copy link
Member

Choose a reason for hiding this comment

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

Psalm is right. Let's fix that: #42183

}

if (204 !== $statusCode) {
$result = $response->toArray(false);

if (401 === $response->getStatusCode()) {
if (401 === $statusCode) {
$originalContent = $message->getSubject();
$errorMessage = $result['message'];
$errorCode = $result['code'];
throw new TransportException(sprintf('Unable to post the Discord message: "%s" (%d: "%s").', $originalContent, $errorCode, $errorMessage), $response);
}

if (400 === $response->getStatusCode()) {
if (400 === $statusCode) {
$originalContent = $message->getSubject();

$errorMessage = '';
Expand All @@ -100,7 +107,7 @@ protected function doSend(MessageInterface $message): SentMessage
throw new TransportException(sprintf('Unable to post the Discord message: "%s" (%s).', $originalContent, $errorMessage), $response);
}

throw new TransportException(sprintf('Unable to post the Discord message: "%s" (Status Code: %d).', $message->getSubject(), $response->getStatusCode()), $response);
throw new TransportException(sprintf('Unable to post the Discord message: "%s" (Status Code: %d).', $message->getSubject(), $statusCode), $response);
}

return new SentMessage($message, (string) $this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -75,11 +76,17 @@ protected function doSend(MessageInterface $message): SentMessage
],
]);

if (200 === $response->getStatusCode()) {
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Esendex server.', $response, 0, $e);
}

if (200 === $statusCode) {
return new SentMessage($message, (string) $this);
}

$message = sprintf('Unable to send the SMS: error %d.', $response->getStatusCode());
$message = sprintf('Unable to send the SMS: error %d.', $statusCode);

try {
$result = $response->toArray(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -74,10 +75,16 @@ protected function doSend(MessageInterface $message): SentMessage
'json' => array_filter($options),
]);

try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Forebase server.', $response, 0, $e);
}

$contentType = $response->getHeaders(false)['content-type'][0] ?? '';
$jsonContents = 0 === strpos($contentType, 'application/json') ? $response->toArray(false) : null;

if (200 !== $response->getStatusCode()) {
if (200 !== $statusCode) {
$errorMessage = $jsonContents ? $jsonContents['results']['error'] : $response->getContent(false);

throw new TransportException('Unable to post the Firebase message: '.$errorMessage, $response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -68,15 +69,21 @@ protected function doSend(MessageInterface $message): SentMessage
],
]);

if (200 !== $response->getStatusCode()) {
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote FreeMobile server.', $response, 0, $e);
}

if (200 !== $statusCode) {
$errors = [
400 => 'Missing required parameter or wrongly formatted message.',
402 => 'Too many messages have been sent too fast.',
403 => 'Service not enabled or wrong credentials.',
500 => 'Server error, please try again later.',
];

throw new TransportException(sprintf('Unable to send the SMS: error %d: ', $response->getStatusCode()).($errors[$response->getStatusCode()] ?? ''), $response);
throw new TransportException(sprintf('Unable to send the SMS: error %d: ', $statusCode).($errors[$statusCode] ?? ''), $response);
}

return new SentMessage($message, (string) $this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -121,14 +122,20 @@ protected function doSend(MessageInterface $message): SentMessage
'json' => array_filter($options),
]);

try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote GoogleChat server.', $response, 0, $e);
}

try {
$result = $response->toArray(false);
} catch (JsonException $jsonException) {
throw new TransportException('Unable to post the Google Chat message: Invalid response.', $response, $response->getStatusCode(), $jsonException);
throw new TransportException('Unable to post the Google Chat message: Invalid response.', $response, $statusCode, $jsonException);
}

if (200 !== $response->getStatusCode()) {
throw new TransportException(sprintf('Unable to post the Google Chat message: "%s".', $result['error']['message'] ?? $response->getContent(false)), $response, $result['error']['code'] ?? $response->getStatusCode());
if (200 !== $statusCode) {
throw new TransportException(sprintf('Unable to post the Google Chat message: "%s".', $result['error']['message'] ?? $response->getContent(false)), $response, $result['error']['code'] ?? $statusCode);
}

if (!\array_key_exists('name', $result)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -76,7 +77,13 @@ protected function doSend(MessageInterface $message): SentMessage
],
]);

if (200 !== $response->getStatusCode()) {
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Infobip server.', $response, 0, $e);
}

if (200 !== $statusCode) {
$content = $response->toArray(false);
$errorMessage = $content['requestError']['serviceException']['messageId'] ?? '';
$errorInfo = $content['requestError']['serviceException']['text'] ?? '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -81,7 +82,13 @@ protected function doSend(MessageInterface $message): SentMessage
'json' => array_filter($opts ? $opts->toArray() : $this->bodyFromMessageWithNoOption($message)),
]);

if (201 !== $response->getStatusCode()) {
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote LinkedIn server.', $response, 0, $e);
}

if (201 !== $statusCode) {
throw new TransportException(sprintf('Unable to post the Linkedin message: "%s".', $response->getContent(false)), $response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -73,7 +74,13 @@ protected function doSend(MessageInterface $message): SentMessage
'json' => array_filter($options),
]);

if (201 !== $response->getStatusCode()) {
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Mattermost server.', $response, 0, $e);
}

if (201 !== $statusCode) {
$result = $response->toArray(false);

throw new TransportException(sprintf('Unable to post the Mattermost message: %s (%s).', $result['message'], $result['id']), $response);
Expand Down
11 changes: 9 additions & 2 deletions 11 src/Symfony/Component/Notifier/Bridge/Mobyt/MobytTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -81,11 +82,17 @@ protected function doSend(MessageInterface $message): SentMessage
'body' => json_encode(array_filter($options)),
]);

if (401 === $response->getStatusCode() || 404 === $response->getStatusCode()) {
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Mobyt server.', $response, 0, $e);
}

if (401 === $statusCode || 404 === $statusCode) {
throw new TransportException(sprintf('Unable to send the SMS: "%s". Check your credentials.', $message->getSubject()), $response);
}

if (201 !== $response->getStatusCode()) {
if (201 !== $statusCode) {
$error = $response->toArray(false);

throw new TransportException(sprintf('Unable to send the SMS: "%s".', $error['result']), $response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -68,7 +69,12 @@ protected function doSend(MessageInterface $message): SentMessage
],
]);

$result = $response->toArray(false);
try {
$result = $response->toArray(false);
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Nexmo server.', $response, 0, $e);
}

foreach ($result['messages'] as $msg) {
if ($msg['status'] ?? false) {
throw new TransportException('Unable to send the SMS: '.$msg['error-text'].sprintf(' (code %s).', $msg['status']), $response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -88,7 +89,13 @@ protected function doSend(MessageInterface $message): SentMessage
'body' => $body,
]);

if (200 !== $response->getStatusCode()) {
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote OvhCloud server.', $response, 0, $e);
}

if (200 !== $statusCode) {
$error = $response->toArray(false);

throw new TransportException(sprintf('Unable to send the SMS: %s.', $error['message']), $response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -81,7 +82,13 @@ protected function doSend(MessageInterface $message): SentMessage
]
);

if (200 !== $response->getStatusCode()) {
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote RocketChat server.', $response, 0, $e);
}

if (200 !== $statusCode) {
throw new TransportException(sprintf('Unable to post the RocketChat message: %s.', $response->getContent(false)), $response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -67,7 +68,13 @@ protected function doSend(MessageInterface $message): SentMessage
],
]);

if (201 !== $response->getStatusCode()) {
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Sendinblue server.', $response, 0, $e);
}

if (201 !== $statusCode) {
$error = $response->toArray(false);

throw new TransportException('Unable to send the SMS: '.$error['message'], $response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -68,7 +69,13 @@ protected function doSend(MessageInterface $message): SentMessage
],
]);

if (201 !== $response->getStatusCode()) {
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Sinch server.', $response, 0, $e);
}

if (201 !== $statusCode) {
$error = $response->toArray(false);

throw new TransportException(sprintf('Unable to send the SMS: %s (%s).', $error['text'], $error['code']), $response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -85,7 +86,13 @@ protected function doSend(MessageInterface $message): SentMessage
],
]);

if (200 !== $response->getStatusCode()) {
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Slack server.', $response, 0, $e);
}

if (200 !== $statusCode) {
throw new TransportException(sprintf('Unable to post the Slack message: "%s".', $response->getContent(false)), $response);
}

Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.