Reputation: 1639
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))
Upvotes: 1
Views: 12565
Reputation: 1
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
.
Upvotes: 0
Reputation: 82
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.
Upvotes: 1
Reputation: 359
The exception that is fired is a ServerException instance, and catch block tries to catch ClientException.
} catch (GuzzleHttp\Exception\ServerException $e) {
Upvotes: 2
Reputation: 111899
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.
Upvotes: 6