bcosca / fatfree

A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!
2.66k stars 446 forks source link

imap_open() failure (WARNING level) results in script halt #1205

Closed MDBenson closed 3 years ago

MDBenson commented 4 years ago

Hi,

I am trying to integrate IMAP features into an established FatFree 3.6 project and am running into a problem. Upon failure to connect, imap_open() throws a WARNING level message as below:

Internal Server Error

imap_open(): Couldn't open stream {some.server.com/imap/ssl}Mailbox

[/var/famousfour/php/web-order-2/src/vendor/bcosca/fatfree-core/base.php:2261] Base->error()

I can't see that this error is trappable using a try/catch as it's not an Exception. All my attempts to trap and report it so far have failed and my script stops on a 500 Internal Server Error genrated by FatFree's Error Handler (Base::error()). Is there any way to configure FatFree so I can handle this better? I would prefer this failed to a graceful Exception that I could trap and return to the user.

My only solution so far is to set then restore the error handler thus:

\set_error_handler(function() {
    $lastError = \imap_last_error();

    throw new Exceptions\ConnectionException("IMAP connection could not be established: " . $lastError);
});

$connect = \imap_open($mailbox, $username, $password, $options, $n_retries, $params);

\restore_error_handler();

But this is a bit clunky and also impractical as I am using a composer package (https://github.com/barbushin/php-imap) for PHP IMAP functions and editing the package files becomes a problem for future updates to the php-imap package.

ikkez commented 3 years ago

When the function throws a Fatal error you cannot catch it with an expection. If it's a Notice, you can change the error_reporting to exclude notices and silence the error with an @ prefix. I found similar issues on SO about imap... maybe this helps: https://stackoverflow.com/questions/42223245/imap-open-custom-error-callback?rq=1 https://stackoverflow.com/questions/5422405/cant-silence-imap-open-error-notices-in-php