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

#571: Improve ConnectionException handling #652

Merged

Conversation

Sebbo94BY
Copy link
Collaborator

This change solves the issue, that the original IMAP connect error gets masked.

ConnectionException::getErrors($select = 'first') is a new function, which allows to get either all IMAP errors, only the first or last reported error.

ConnectionException::getMessage() is still simply returning a string. This string contains now all IMAP errors as JSON encoded format. The implementation allows further options for the future, if necessary.

Example code to test:

<?php
    require_once __DIR__.'/../vendor/autoload.php';

    use PhpImap\Exceptions\ConnectionException;
    use PhpImap\Mailbox;

    $mailbox = new Mailbox(
        '{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
        'some@gmail.com', // Username for the before configured mailbox
        'wrongPassword', // Password for the before configured username
        __DIR__, // Directory, where attachments will be saved (optional)
        'US-ASCII' // Server encoding (optional)
    );

    try {
        $mail_ids = $mailbox->searchMailbox('ALL');
    } catch (ConnectionException $ex) {
        // Get first reported IMAP error
        exit('IMAP connection failed: '.$ex->getErrors()); // default behaviour
        exit('IMAP connection failed: '.$ex->getErrors('first'));

        // Get last reported IMAP error
        exit('IMAP connection failed: '.$ex->getErrors('last'));

        // Get all reported IMAP errors (Array)
        exit('IMAP connection failed: '.implode(",", $ex->getErrors('all')));

        // Get all reported IMAP errors (JSON encoded string)
        exit('IMAP connection failed: '.$ex->getMessage());
    } catch (Exception $ex) {
        exit('An error occured: '.$ex->getMessage());
    }

Fixes #571.

@Sebbo94BY Sebbo94BY merged commit 6332a9c into master Jan 7, 2022
@Sebbo94BY Sebbo94BY deleted the 571-imap_open-throws-exception-and-masks-real-error branch January 7, 2022 00:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

imap_open throws \ErrorException and masks real error
1 participant