kreait / firebase-bundle

A Symfony Bundle for the Firebase PHP Admin SDK
https://github.com/kreait/firebase-php
MIT License
136 stars 26 forks source link

FirebaseException not caught by try/catch? #4

Closed ktwbc closed 8 years ago

ktwbc commented 8 years ago

Would you have any idea why FirebaseException which is just an extension of Exception wouldn't be caught in a regular try/catch?

About 1 out of 10 times I get a timeout to Firebase on this code:

protected function updateFirebase($file_no, $line_no)
    {
      echo "*updating firebase *\n";
        try {
            /** @var Firebase $firebase */
            $firebase = $this->firebase;

            $event = $firebase->getReference('dataprocessing');

            if ($file_no == 0) {
                $msg = "Processing finished";
            } else {
                $ttl = 10000 * 5;
                $prior = ($file_no - 1) * 10000;

                $current = round(100 * ($prior + $line_no) / $ttl);

                $msg = "Processing " . $current . "%";
            }

            $event->set([$msg]);

        } catch (Exception $objExc) {
            $this->logger->info('Error on Firebase ' . $objExc->getMessage() . ' ' . __CLASS__ . "." . __FUNCTION__);
        }

    }

However when the timeout occurs, it always breaks and halts the program:

updating firebase

[Kreait\Firebase\Exception\FirebaseException]
HTTP Error: An error occurred when fetching the URI "https://mytestaccount.firebaseio.com/point2.json" with the adapter "guzzle_http" ("cURL error 28: Operation timed out after 10001 milliseconds with 0 bytes received").

[Ivory\HttpAdapter\HttpAdapterException]
An error occurred when fetching the URI "https://mytestaccount.firebaseio.com/point2.json" with the adapter "guzzle_http" ("cURL error 28: Operation timed out after 10001 milliseconds with 0 bytes received").

Trying to figure out what about this library would ignore the error trap.

jeromegamez commented 8 years ago

Interesting problem... could you try catching \Exception $objExc instead of Exception $objExc (with a leading backslash)? It could be that you already have another exception class named Exception in the current namespace or in one of the namespaces imported with use … statements.

ktwbc commented 8 years ago

Argh, you called it exactly. Somehow PHPStorm was being helpful and autocreated

use Symfony\Component\Security\Acl\Exception\Exception;

picking the wrong Exception instead of \Exception and so the trap wasn't getting it. I didn't even see that use statement.