XF 2.0 Can't catch Guzzle request exceptions?

KevinP

New member
I'm working on an addon and have a controller which makes a POST request to an external service that I'm also building. I'm attempting to make it so if Xenforo sends a request to that service and that service is offline or otherwise causes Guzzle to throw a RequestException and display the red error window to the browser to not do that, and instead just do nothing if the request fails. The action function looks similar to this:

PHP:
public function actionRegister(ParameterBag $params) {
    $visitor = \XF::visitor();
    $client = \XF::app()->http()->client();
    $success = false;
   
    try {
        $res = $client->post('http://127.0.0.1:9998', [
            'body' => [
                'user_id' => $visitor->user_id
            ]
        ]);
       
        $success = true;
    } catch (Exception $e) {
        // Do nothing.
    }
   
    $viewParams = [
        "success" => $success,
    ];
   
    return $this->view('AddOn:Index', 'addon_index', $viewParams);
}

Even with that try/catch in place, I still am shown the red error window. I've also tried catching RequestException instead of just Exception, and setting the request's 'http_errors' variable to false - neither of which stopped Xenforo from catching the error. Is there any way to override Xenforo's default error handling?
 
..on the same topic, I've tried this before as well and it didn't work.

I'm sure I have something wrong.

PHP:
catch(\GuzzleHttp\Exception\RequestException $e)
        {
            throw new \LogicException("API Call failed! Are your options corrrect?" . $e);
        }
 
...that does. I would've thought it would complain about just using RequestException as if it couldn't find the class, or have worked if I used that directive within a use statement (something else I tried as well). Works nonetheless, thank you!
 
I just use:
Code:
catch (\Exception $e)
Seems to work fine.
Don't do this, this is bad form. You should always aim to catch the specific exceptions that are being thrown by the method that you're calling.

While it will work, of course, since every single custom exception will always inherit from \Exception, getting used to doing this will create bad habits.

The reason why the code in the OP doesn't work is that @KevinP is missing the \ in front of Exception. If you do not precede Exception with a slash, it will be taken as a class inside your current namespace, e.g. Your\App\Some\Namespace\Exception, which is not the exception being thrown by Guzzle.


Fillip
 
Top Bottom