Skip to content

Commit

Permalink
handle inpi exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jordankanta committed Jan 19, 2024
1 parent 69c6be8 commit 09baa54
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
18 changes: 18 additions & 0 deletions src/INPIException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace RNEClient;

class INPIException extends \Exception
{
const BAD_REQUEST_ERROR = 'The request is invalid.';
const BAD_CREDENTIALS_ERROR = 'The user credentials are invalid.';
const FORBIDDEN_ERROR = 'The user does not have the necessary authorizations to make the request.';
const TOO_MANY_REQUESTS_ERROR = 'The user has exceeded the daily quotas.';
const INTERNAL_SERVER_ERROR = 'The server encountered an unexpected error.';

public function __construct($message, $code = 0, \Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}

}
10 changes: 5 additions & 5 deletions src/RNEClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ protected function requestApi(string $method, string $url, array $options = []):
protected function catchResponseErrors(GuzzleException $e): void
{
if ($e->getCode() === 400) {
throw new \Exception('Bad request');
throw new INPIException(INPIException::BAD_REQUEST_ERROR, 400, $e);
} elseif ($e->getCode() === 401) {
throw new \Exception('Bad credentials');
throw new INPIException(INPIException::BAD_CREDENTIALS_ERROR, 401, $e);
} elseif ($e->getCode() === 403) {
throw new \Exception('Forbidden');
throw new INPIException(INPIException::FORBIDDEN_ERROR, 403, $e);
} elseif ($e->getCode() === 429) {
throw new \Exception('Too many requests');
throw new INPIException(INPIException::TOO_MANY_REQUESTS_ERROR, 429, $e);
} elseif ($e->getCode() === 500) {
throw new \Exception('Internal server error');
throw new INPIException(INPIException::INTERNAL_SERVER_ERROR, 500, $e);
} else {
throw new \Exception('Unknown error', 0, $e);
}
Expand Down
20 changes: 10 additions & 10 deletions tests/RNEClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public function testAuthenticateWithBadRequest(): void
$this->RNEClient = new RNEClient(null, $mockedClient);

// Testez que l'exception est bien levée lors de l'authentification avec de mauvais identifiants
$this->expectException(\Exception::class);
$this->expectExceptionMessage("Bad request");
$this->expectException(INPIException::class);
$this->expectExceptionMessage(INPIException::BAD_REQUEST_ERROR);

$this->RNEClient->authenticate('fake_username', 'fake_password');
}
Expand Down Expand Up @@ -88,8 +88,8 @@ public function testAuthenticationWithBadCredentials(): void
$this->RNEClient = new RNEClient(null, $mockedClient);

// Testez que l'exception est bien levée lors de l'authentification avec de mauvais identifiants
$this->expectException(\Exception::class);
$this->expectExceptionMessage("Bad credentials");
$this->expectException(INPIException::class);
$this->expectExceptionMessage(INPIException::BAD_CREDENTIALS_ERROR);

$this->RNEClient->authenticate('fake_username', 'fake_password');
}
Expand All @@ -108,8 +108,8 @@ public function testAuthenticationWithForbidden(): void
$this->RNEClient = new RNEClient(null, $mockedClient);

// Testez que l'exception est bien levée lors de l'authentification avec de mauvais identifiants
$this->expectException(\Exception::class);
$this->expectExceptionMessage("Forbidden");
$this->expectException(INPIException::class);
$this->expectExceptionMessage(INPIException::FORBIDDEN_ERROR);

$this->RNEClient->authenticate('forbidden_username', 'forbidden_password');
}
Expand All @@ -128,8 +128,8 @@ public function testAuthenticationWithTooManyRequests(): void
$this->RNEClient = new RNEClient(null, $mockedClient);

// Testez que l'exception est bien levée lors de l'authentification avec de mauvais identifiants
$this->expectException(\Exception::class);
$this->expectExceptionMessage("Too many requests");
$this->expectException(INPIException::class);
$this->expectExceptionMessage(INPIException::TOO_MANY_REQUESTS_ERROR);

$this->RNEClient->authenticate('too_many_requests_username', 'too_many_requests_password');
}
Expand All @@ -148,8 +148,8 @@ public function testAuthenticateWithInternalErrorException(): void
$this->RNEClient = new RNEClient(null, $mockedClient);

// Testez que l'exception est bien levée lors de l'authentification avec de mauvais identifiants
$this->expectException(\Exception::class);
$this->expectExceptionMessage("Internal server error");
$this->expectException(INPIException::class);
$this->expectExceptionMessage(INPIException::INTERNAL_SERVER_ERROR);

$this->RNEClient->authenticate('unknow_exception_username', 'unknow_exception_password');
}
Expand Down
4 changes: 2 additions & 2 deletions tests/features/SearchCompanies/SearchCompanyBySirenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function testSearchCompanyBySirenWithApiError(): void

$this->RNEClient = new SearchCompanies('fake_token', $mockedClient);

$this->expectException(\Exception::class);
$this->expectExceptionMessage("Internal server error");
$this->expectException(INPIException::class);
$this->expectExceptionMessage(INPIException::INTERNAL_SERVER_ERROR);
$this->RNEClient->searchBySiren('889924320');
}

Expand Down

0 comments on commit 09baa54

Please sign in to comment.