Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid (set|restor)_error_handler #239

Merged
merged 1 commit into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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