Skip to content

Commit

Permalink
UnexpectedResponseCodeException that bears more information
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 1, 2018
1 parent fb04ed5 commit c9cbb06
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Smtp/AsyncSmtpConnectionWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private function processDataResponse(string $data): void
if (in_array(trim($data), $tooManyMessagesData, true)) {
$exception = new \AsyncConnection\Smtp\TooManyMessagesException($errorMessage);
} else {
$exception = new \AsyncConnection\Smtp\AsyncSmtpConnectionException($errorMessage);
$exception = new \AsyncConnection\Smtp\UnexpectedResponseCodeException($code, $expectedCodes, $errorMessage, $data);
}

$deferred->reject($exception);
Expand Down
45 changes: 45 additions & 0 deletions src/Smtp/UnexpectedResponseCodeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace AsyncConnection\Smtp;

class UnexpectedResponseCodeException extends \AsyncConnection\AsyncConnectionException
{

/** @var int */
private $actualCode;

/** @var int[] */
private $expectedCodes;

/**
* @param int $actualCode
* @param int[] $expectedCodes
* @param string $errorMessage
* @param string $responseMessage
*/
public function __construct(
int $actualCode,
array $expectedCodes,
string $errorMessage,
string $responseMessage
)
{
parent::__construct(sprintf('%s: %s', $errorMessage, $responseMessage));
$this->actualCode = $actualCode;
$this->expectedCodes = $expectedCodes;
}

public function getActualCode(): int
{
return $this->actualCode;
}

/**
* @return int[]
*/
public function getExpectedCodes(): array
{
return $this->expectedCodes;
}

}
8 changes: 4 additions & 4 deletions tests/Smtp/AsyncSmtpConnectionWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,20 @@ public function dataFailedWrites(): array
new AsyncDoubleResponseMessage('EHLO slevomat.local', [SmtpCode::SERVICE_READY], [SmtpCode::OK]),
'421 Service not available',
null,
'SMTP server did not accept message EHLO slevomat.local. Expected code: 220. Actual code: 421.',
'SMTP server did not accept message EHLO slevomat.local. Expected code: 220. Actual code: 421.: 421 Service not available',
],
//invalid second response
[
new AsyncDoubleResponseMessage('EHLO slevomat.local', [SmtpCode::SERVICE_READY], [SmtpCode::OK]),
'220 mailtrap.io ESMTP ready',
'421 Service not available',
'SMTP server did not accept message EHLO slevomat.local. Expected code: 250. Actual code: 421.',
'SMTP server did not accept message EHLO slevomat.local. Expected code: 250. Actual code: 421.: 421 Service not available',
],
[
new AsyncSingleResponseMessage('AUTH LOGIN', [SmtpCode::AUTH_CONTINUE]),
'421 Service not available',
null,
'SMTP server did not accept message AUTH LOGIN. Expected code: 334. Actual code: 421.',
'SMTP server did not accept message AUTH LOGIN. Expected code: 334. Actual code: 421.: 421 Service not available',
],
[
new AsyncSingleResponseMessage('AUTH LOGIN', [SmtpCode::AUTH_CONTINUE]),
Expand All @@ -226,7 +226,7 @@ public function dataFailedWrites(): array
new AsyncSingleResponseMessage('base64EncodedUsername', [SmtpCode::AUTH_CONTINUE], 'credentials'),
'421 Service not available',
null,
'SMTP server did not accept credentials. Expected code: 334. Actual code: 421.',
'SMTP server did not accept credentials. Expected code: 334. Actual code: 421.: 421 Service not available',
],
];
}
Expand Down

0 comments on commit c9cbb06

Please sign in to comment.