7

I have a method that checks if a user has valid Session info. This is supposed to throw an Exception, Guzzle\Http\Exception\BadResponseException but when I try to catch it :

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

Laravel doesn't get to this code and immediately starts it's own error handling. And ideas on how to bypass Laravels own implementation and use my own Catch.

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

EDIT 2:

I sort of fixed the issue by disabling Guzzle exceptions and checking the return header manually. It's a bit of a short cut but in this case, it does the job. Thanks for the responses!

1
  • Can you post more relevant code (including try block) Commented Oct 15, 2013 at 20:06

3 Answers 3

9

Actually this exception can be catched in Laravel, you just have to respect (and understand) namespacing:

If you have

namespace App;

and you do

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

PHP understands that you are trying to

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

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

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

And it will work.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow!! So you should mind namespacing catch(Exception e) won't work it should be catch(\Exception e)
2

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

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

Also, make sure you have a well defined (BadResponseException) class. Read more on Laravel Documentation.

7 Comments

Note: that Laravel will continue to pass the responsibility of handling the error to its registered handlers until one returns a Response object. Laravel uses the Chain of Responsibility pattern when handling errors. This lets you handle the error any way you choose. You can do something like log the Guzzle error, and then let Laravel's other error handlers decide how to respond. See this blog post for some more explanation.
This is not an answer to "how to bypass it?" and handle the exception in my method right after I called something that might throw some exceptions. Why should I write code outside my class to handle things that my class know how to handle? Take a look at some Sentry's Cartalyst examples: docs.cartalyst.com/sentry-2/authentication/login. Those are stuff that will never work on Laravel.
@AntonioCarlosRibeiro, the question I thought about how to register your own error handling, am I wrong ?
As he/she says "but when I try to catch it:". Imo, as the code shows, he's trying to catch the exception in a method which cannot succeed because Laravel catches it before anything else.
@AntonioCarlosRibeiro, OH! i see now, OP is using try catch in the class itself.
|
2

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 possible exceptions thrown by Guzzle.

If you explicitly want to catch a BadResponseException you can also prepend your exception's class namespace with '\'.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.