Skip to content

Commit 6332a9c

Browse files
authored
Merge pull request #652 from barbushin/571-imap_open-throws-exception-and-masks-real-error
#571: Improve ConnectionException handling
2 parents ed0ecf1 + f85dcc9 commit 6332a9c

File tree

6 files changed

+39
-16
lines changed

6 files changed

+39
-16
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Initially released in December 2012, the PHP IMAP Mailbox is a powerful and open
4141
* PHP `iconv` extension must be present; so make sure this line is active in your php.ini: `extension=php_iconv.dll`
4242
* PHP `imap` extension must be present; so make sure this line is active in your php.ini: `extension=php_imap.dll`
4343
* PHP `mbstring` extension must be present; so make sure this line is active in your php.ini: `extension=php_mbstring.dll`
44+
* PHP `json` extension must be present; so make sure this line is active in your php.ini: `extension=json.dll`
4445

4546
### Installation by Composer
4647

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

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"ext-fileinfo": "*",
2828
"ext-iconv": "*",
2929
"ext-imap": "*",
30-
"ext-mbstring": "*"
30+
"ext-mbstring": "*",
31+
"ext-json": "*"
3132
},
3233
"require-dev": {
3334
"friendsofphp/php-cs-fixer": "^3.4",

examples/get_and_parse_unseen_emails.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
try {
2424
$mail_ids = $mailbox->searchMailbox('UNSEEN');
2525
} catch (ConnectionException $ex) {
26-
exit('IMAP connection failed: '.$ex->getMessage());
26+
exit('IMAP connection failed: '.$ex->getErrors('first'));
2727
} catch (Exception $ex) {
2828
exit('An error occured: '.$ex->getMessage());
2929
}

src/PhpImap/Exceptions/ConnectionException.php

+26
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,30 @@
1313
*/
1414
class ConnectionException extends Exception
1515
{
16+
public function __construct($message, $code = 0, Exception $previous = null)
17+
{
18+
parent::__construct(json_encode($message), $code, $previous);
19+
}
20+
21+
public function getErrors($select = 'first')
22+
{
23+
$message = $this->getMessage();
24+
25+
switch (strtolower($select)) {
26+
case 'all':
27+
return json_decode($message);
28+
break;
29+
default:
30+
case 'first':
31+
$message = json_decode($message);
32+
33+
return $message[0];
34+
break;
35+
case 'last':
36+
$message = json_decode($message);
37+
38+
return $message[\count($message) - 1];
39+
break;
40+
}
41+
}
1642
}

src/PhpImap/Imap.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use const IMAP_WRITETIMEOUT;
1515
use InvalidArgumentException;
1616
use const NIL;
17+
use PhpImap\Exceptions\ConnectionException;
1718
use const SE_FREE;
1819
use const SORTARRIVAL;
1920
use const SORTCC;
@@ -707,13 +708,7 @@ public static function open(
707708
$result = @\imap_open($mailbox, $username, $password, $options, $n_retries, $params);
708709

709710
if (!$result) {
710-
$lastError = \imap_last_error();
711-
712-
if ((\is_string($lastError)) && ('' !== \trim($lastError))) {
713-
throw new UnexpectedValueException('IMAP error:'.$lastError);
714-
}
715-
716-
throw new UnexpectedValueException('Could not open mailbox!', 0, self::HandleErrors(\imap_errors(), 'imap_open'));
711+
throw new ConnectionException(\imap_errors());
717712
}
718713

719714
return $result;

tests/unit/ImapTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
use Generator;
1010
use ParagonIE\HiddenString\HiddenString;
11+
use PhpImap\Exceptions\ConnectionException;
1112
use PHPUnit\Framework\TestCase as Base;
1213
use const SORTARRIVAL;
1314
use Throwable;
14-
use UnexpectedValueException;
1515

1616
/**
1717
* @psalm-type MAILBOX_ARGS = array{
@@ -35,13 +35,13 @@ class ImapTest extends Base
3535
use LiveMailboxTestingTrait;
3636

3737
/**
38-
* @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>
38+
* @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>
3939
*/
4040
public function OpenFailure(): Generator
4141
{
4242
yield 'empty mailbox/username/password' => [
43-
UnexpectedValueException::class,
44-
'IMAP error:Can\'t open mailbox : no such mailbox',
43+
ConnectionException::class,
44+
'Can\'t open mailbox : no such mailbox',
4545
[
4646
new HiddenString(''),
4747
new HiddenString(''),
@@ -58,8 +58,8 @@ public function OpenFailure(): Generator
5858

5959
if (\is_string($imapPath) && \is_string($login) && \is_string($password)) {
6060
yield 'CI ENV with invalid password' => [
61-
UnexpectedValueException::class,
62-
'/^IMAP error:.*\[AUTHENTICATIONFAILED\].*/',
61+
ConnectionException::class,
62+
'/^\[AUTHENTICATIONFAILED\].*/',
6363
[
6464
new HiddenString($imapPath, true, true),
6565
new HiddenString($login, true, true),

0 commit comments

Comments
 (0)