Skip to content

Commit

Permalink
Merge pull request #652 from barbushin/571-imap_open-throws-exception…
Browse files Browse the repository at this point in the history
…-and-masks-real-error

#571: Improve ConnectionException handling
  • Loading branch information
Sebbo94BY authored Jan 7, 2022
2 parents ed0ecf1 + f85dcc9 commit 6332a9c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Initially released in December 2012, the PHP IMAP Mailbox is a powerful and open
* PHP `iconv` extension must be present; so make sure this line is active in your php.ini: `extension=php_iconv.dll`
* PHP `imap` extension must be present; so make sure this line is active in your php.ini: `extension=php_imap.dll`
* PHP `mbstring` extension must be present; so make sure this line is active in your php.ini: `extension=php_mbstring.dll`
* PHP `json` extension must be present; so make sure this line is active in your php.ini: `extension=json.dll`

### Installation by Composer

Expand Down Expand Up @@ -97,7 +98,7 @@ try {
// PHP.net imap_search criteria: http://php.net/manual/en/function.imap-search.php
$mailsIds = $mailbox->searchMailbox('ALL');
} catch(PhpImap\Exceptions\ConnectionException $ex) {
echo "IMAP connection failed: " . $ex;
echo "IMAP connection failed: " . implode(",", $ex->getErrors('all'));
die();
}

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"ext-fileinfo": "*",
"ext-iconv": "*",
"ext-imap": "*",
"ext-mbstring": "*"
"ext-mbstring": "*",
"ext-json": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.4",
Expand Down
2 changes: 1 addition & 1 deletion examples/get_and_parse_unseen_emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
try {
$mail_ids = $mailbox->searchMailbox('UNSEEN');
} catch (ConnectionException $ex) {
exit('IMAP connection failed: '.$ex->getMessage());
exit('IMAP connection failed: '.$ex->getErrors('first'));
} catch (Exception $ex) {
exit('An error occured: '.$ex->getMessage());
}
Expand Down
26 changes: 26 additions & 0 deletions src/PhpImap/Exceptions/ConnectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,30 @@
*/
class ConnectionException extends Exception
{
public function __construct($message, $code = 0, Exception $previous = null)
{
parent::__construct(json_encode($message), $code, $previous);
}

public function getErrors($select = 'first')
{
$message = $this->getMessage();

switch (strtolower($select)) {
case 'all':
return json_decode($message);
break;
default:
case 'first':
$message = json_decode($message);

return $message[0];
break;
case 'last':
$message = json_decode($message);

return $message[\count($message) - 1];
break;
}
}
}
9 changes: 2 additions & 7 deletions src/PhpImap/Imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use const IMAP_WRITETIMEOUT;
use InvalidArgumentException;
use const NIL;
use PhpImap\Exceptions\ConnectionException;
use const SE_FREE;
use const SORTARRIVAL;
use const SORTCC;
Expand Down Expand Up @@ -707,13 +708,7 @@ public static function open(
$result = @\imap_open($mailbox, $username, $password, $options, $n_retries, $params);

if (!$result) {
$lastError = \imap_last_error();

if ((\is_string($lastError)) && ('' !== \trim($lastError))) {
throw new UnexpectedValueException('IMAP error:'.$lastError);
}

throw new UnexpectedValueException('Could not open mailbox!', 0, self::HandleErrors(\imap_errors(), 'imap_open'));
throw new ConnectionException(\imap_errors());
}

return $result;
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/ImapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

use Generator;
use ParagonIE\HiddenString\HiddenString;
use PhpImap\Exceptions\ConnectionException;
use PHPUnit\Framework\TestCase as Base;
use const SORTARRIVAL;
use Throwable;
use UnexpectedValueException;

/**
* @psalm-type MAILBOX_ARGS = array{
Expand All @@ -35,13 +35,13 @@ class ImapTest extends Base
use LiveMailboxTestingTrait;

/**
* @psalm-return Generator<'CI ENV with invalid password'|'empty mailbox/username/password', array{0: UnexpectedValueException::class, 1: '/^IMAP error:.[AUTHENTICATIONFAILED]/'|'IMAP error:Can't open mailbox : no such mailbox', 2: array{0: HiddenString, 1: HiddenString, 2: HiddenString, 3: 0, 4: 0, 5: array<empty, empty>}, 3?: true}, mixed, void>
* @psalm-return Generator<'CI ENV with invalid password'|'empty mailbox/username/password', array{0: ConnectionException::class, 1: '/^[AUTHENTICATIONFAILED]/'|'Can't open mailbox : no such mailbox', 2: array{0: HiddenString, 1: HiddenString, 2: HiddenString, 3: 0, 4: 0, 5: array<empty, empty>}, 3?: true}, mixed, void>
*/
public function OpenFailure(): Generator
{
yield 'empty mailbox/username/password' => [
UnexpectedValueException::class,
'IMAP error:Can\'t open mailbox : no such mailbox',
ConnectionException::class,
'Can\'t open mailbox : no such mailbox',
[
new HiddenString(''),
new HiddenString(''),
Expand All @@ -58,8 +58,8 @@ public function OpenFailure(): Generator

if (\is_string($imapPath) && \is_string($login) && \is_string($password)) {
yield 'CI ENV with invalid password' => [
UnexpectedValueException::class,
'/^IMAP error:.*\[AUTHENTICATIONFAILED\].*/',
ConnectionException::class,
'/^\[AUTHENTICATIONFAILED\].*/',
[
new HiddenString($imapPath, true, true),
new HiddenString($login, true, true),
Expand Down

0 comments on commit 6332a9c

Please sign in to comment.