Skip to content

Commit

Permalink
Avoid (set|restor)_error_handler (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk authored Oct 11, 2017
1 parent a2f8185 commit 4dccfd9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
16 changes: 9 additions & 7 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ public function __construct(ImapResourceInterface $resource, int $messageNumber)
*/
private function loadStructure(ImapResourceInterface $resource, int $messageNumber): \stdClass
{
\set_error_handler(function ($nr, $error) use ($messageNumber) {
throw new MessageDoesNotExistException(\sprintf('Message "%s" does not exist: %s', $messageNumber, $error), $nr);
});

$structure = \imap_fetchstructure(
\error_clear_last();
$structure = @\imap_fetchstructure(
$resource->getStream(),
$messageNumber,
\FT_UID
);

\restore_error_handler();
if (null !== ($lastError = \error_get_last())) {
throw new MessageDoesNotExistException(\sprintf(
'Message "%s" does not exist: %s',
$messageNumber,
$lastError['message']
), $lastError['type']);
}

if (!$structure instanceof \stdClass) {
throw new MessageStructureException(\sprintf('Message "%s" structure is empty', $messageNumber));
Expand Down
17 changes: 9 additions & 8 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,22 @@ public function __construct(
*/
public function authenticate(string $username, string $password): ConnectionInterface
{
// Wrap imap_open, which gives notices instead of exceptions
\set_error_handler(function ($nr, $message) use ($username) {
throw new AuthenticationFailedException(\sprintf('Authentication failed for user "%s": %s', $username, $message), $nr);
});

$resource = \imap_open(
\error_clear_last();
$resource = @\imap_open(
$this->getServerString(),
$username,
$password,
0,
1,
$this->parameters
);

\restore_error_handler();
if (null !== ($lastError = \error_get_last())) {
throw new AuthenticationFailedException(\sprintf(
'Authentication failed for user "%s": %s',
$username,
$lastError['message']
), $lastError['type']);
}

if (false === $resource) {
throw new AuthenticationFailedException(\sprintf('Authentication failed for user "%s"', $username));
Expand Down

0 comments on commit 4dccfd9

Please sign in to comment.