Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error with Guzzle ClientException #13

Open
Colir opened this issue Sep 5, 2023 · 1 comment
Open

Fatal error with Guzzle ClientException #13

Colir opened this issue Sep 5, 2023 · 1 comment

Comments

@Colir
Copy link

Colir commented Sep 5, 2023

Hi.
I've just installed the Brevo sdk.
All work great, but when the API v3 returns a bad request, this triggers a fatal error by Guzzle

here is my log example

PHP Fatal error:  Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://api.brevo.com/v3/contacts` resulted in a `400 Bad Request` response:
{"code":"duplicate_parameter","message":"Unable to update contact, SMS or WHATSAPP are already associated with another C (truncated...)

and here is my code

 $response = $this->client->request('POST', 'https://api.brevo.com/v3/contacts', [ 
            'body' => json_encode($contact),
            'headers' => [
                'accept' => 'application/json',
                'api-key' => BREVO_API_KEY,
                'content-type' => 'application/json',
            ],
        ]);

 try {
	$statusCode = $response->getStatusCode();
	$responseBody = $response->getBody();
	$responseArray = json_decode($responseBody,true);

	if( $statusCode == 201){
		return array('success' => true, 'message' =>  $responseArray);
	} else {
		return array('success' => false, 'message' => $responseArray['message']);
	}

 } catch (ClientException $e) {
            write_log('client exception');
            write_log($e->getMessage());
        
}catch (RequestException $e) {
            write_log('request exception');
            write_log($e->getMessage());  

} catch (Exception $e) {
	write_log($e->getMessage());
}

Does anyone can help me to catch this error ?

thank you

@Colir
Copy link
Author

Colir commented Sep 13, 2023

Here is the solution:
As the exception occurs on the Client, the first
try{}
must wrap the $response variable

try{ $response = $this->client->request(....) } catch ( Exception $e ){ .... }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant