1

I need to connect to an API so I write a function:

try {
    $res4 = $client3->post('https://api.example.co.uk/Book', [
        'headers' => [
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer ajhsdbjhasdbasdbasd',
        ],
        'json' => [
            'custFirstName' => $FirstName,
            'custLastName' => $Surname,
            'custPhone' => $Mobile,
            'custEmail' => $Email,
        ]
    ]);
} catch (GuzzleHttp\Exception\ClientException $e) {
    $response = $e->getResponse();
    $result = json_decode($response->getBody()->getContents());
    $item->update(['status' => 'Problems at step3']);
    Mail::raw('Problem at STEP 3', function ($message) use ($serial) {
        $message->from('[email protected]', '[email protected]');
        $message->subject('We got a problem etc.');
        $message->to('[email protected]');
    });
}

As you can see I need to make a call to API but in the case when API is down I write catch functions.

But now when API is down and API return '500 Internal Error' this function is just crashed ...

My question is why catch dont handle it?

How I can handle errors - when API is down or bad request... WHy catch{} doesn't work?

UPDATE: here is my laravel.log

[2018-10-25 14:51:04] local.ERROR: GuzzleHttp\Exception\ServerException: Server error: `POST https://api.example.co.uk/Book` resulted in a `500 Internal Server Error` response:
{"message":"An error has occured. Please contact support."}
 in /home/public_html/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:107
Stack trace:
#0 /home/public_html/vendor/guzzlehttp/guzzle/src/Middleware.php(65): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response))
#1 /home/public_html/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))
18
  • 1
    Please check laravel log in folder /storage/log/laravel.log Commented Oct 25, 2018 at 14:46
  • 2
    It might not be a ClientException catch the root Exception Exception instead Commented Oct 25, 2018 at 14:46
  • Check your log and post the full error here. Commented Oct 25, 2018 at 14:48
  • 500 internal error is not an exception that was thrown by the way. Commented Oct 25, 2018 at 14:49
  • from API i got error: ServerException in RequestException.php line 107: Server error: POST https://api.exampe.co.uk/Book resulted in a 500 Internal Server Error response: {"message":"An error has occured. Please contact support."} Commented Oct 25, 2018 at 14:51

4 Answers 4

6

The problem are namespaces here, instead of:

} catch (GuzzleHttp\Exception\ClientException $e) {

you should rather use:

} catch (\GuzzleHttp\Exception\ClientException $e) {

Otherwise PHP assumes that class is in current namespacase, so in fact when you used GuzzleHttp\Exception\ClientException in fact you probably used App\Http\Controllers\GuzzleHttp\Exception\ClientException and such exception obviously won't be thrown by Guzzle.

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

5 Comments

no, this is not the solution, because I got the same error again. All I want is to catch error from API and update $item, also to send ALERT email to some email address
@AleksPer Oh ok, so in error log you have GuzzleHttp\Exception\ServerException, so you can replace with this one. However because you might have different exceptions, it's better to replace with } catch (\Exception $e) { to really catch any exception that might happen here.
catch (\Exception $e) { will catch any error on API's ? I tried and seems that works good for now
@AleksPer Yes, it should catch any exception that appear. But if you are using PHP 7, even better is to use \Throwable just in case - see php.net/manual/en/class.throwable.php
This worked for me with 3rd party library's Exception.
2

The exception that is fired is a ServerException instance, and catch block tries to catch ClientException.

} catch (GuzzleHttp\Exception\ServerException $e) {

Comments

1

in your app/exceptions/handler.php file, update the render method like this one.

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $exception
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $exception) {
    if ($exception instanceof \GuzzleHttp\Exception\ClientException) {
        return your_response();
    }
    return parent::render($request, $exception);
}

This approach worked for me.

Comments

0

The issue is you're trying to catch an exception, when you should actually be trying to catch an error. In your catch block try using \Error instead of \GuzzleHttp\Exception\ClientException.

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.