How to Bypass Laravel Exception Handling

I have a method that checks if a user has valid session information. It is assumed that you have thrown the Guzzle\Http\Exception\BadResponseException , but when I try to catch it:

 catch (Guzzle\Http\Exception\BadResponseException $e) { return false; } return true 

Laravel does not get to this code and immediately starts its own error handling. And ideas on how to get around Laravels own implementation and use my own Catch.

EDIT: I just found out that Laravel uses the same Exception handler as Symfony, so I also added the Symfony2 tag.

EDIT 2:

I kind of fixed the problem by disabling Guzzle exceptions and checking the return header manually. This is a slightly short cut, but in this case he does the job. Thanks for answers!

+7
php exception symfony laravel guzzle
source share
3 answers

Actually this exception can be found in Laravel, you just need to respect (and understand) the namespace:

If you

 namespace App; 

and you do

 catch (Guzzle\Http\Exception\BadResponseException $e) 

PHP understands that you are trying

 catch (\App\Guzzle\Http\Exception\BadResponseException $e) 

So, for it to work, you just need a slash:

 catch (\Guzzle\Http\Exception\BadResponseException $e) 

And he will work.

+9
source share

By default, the app/start/global.php contains an error handler for all exceptions. However, if necessary, you can specify more handlers. Handlers are called based on the type-hint Exception that they handle. For example, you can create a handler that only handles your BadResponseException instances, for example

 App::error(function(Guzzle\Http\Exception\BadResponseException $exception) { // Handle the exception... return Response::make('Error! ' . $exception->getCode()); }); 

Also, make sure you have a specific ( BadResponseException ) class. Read more about Laravel Documentation .

+2
source share

Instead of your code

 catch (Guzzle\Http\Exception\BadResponseException $e) { return false; } return true 

use this solution

 catch (\Exception $e) { return false; } return true 

to catch all the possible exceptions thrown by Guzzle.

If you explicitly want to catch the BadResponseException, you can also add an exception class namespace using '\' .

 catch (\Guzzle\Http\Exception\BadResponseException $e) { return false; } return true 
+2
source share

All Articles