Skip to content

Commit

Permalink
#571: Improve ConnectionException handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Krätzig committed Jan 7, 2022
1 parent ed0ecf1 commit f4b1716
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
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

0 comments on commit f4b1716

Please sign in to comment.