Skip to content

Commit

Permalink
Merge pull request #9 from bhanwarpsrathore/pradeep/optimization
Browse files Browse the repository at this point in the history
Error handling updated
  • Loading branch information
bhanwarpsrathore authored Feb 15, 2024
2 parents 62c38c3 + 48407a9 commit 2a3334a
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use GrahamCampbell\GuzzleFactory\GuzzleFactory;

class Request {
Expand Down Expand Up @@ -41,30 +42,36 @@ public function __construct(ClientInterface $client = null) {
protected function handleResponseError(string $body, int $status): void {
$parsedBody = json_decode($body);
$errors = $parsedBody->errors ?? null;
$error_message = $errors[0]->status == 401 ? $errors[0]->detail : null;
$user_message = $errors[0]->title ?? null;

if ($error_message) {
if (isset($errors[0]) && isset($errors[0]->detail)) {
// It's an Auth error
throw new LabelcampAPIException($this->parseError($error_message), $status);
} elseif (isset($parsedBody->error_description) && is_string($parsedBody->error)) {
// It's an auth call error
throw new LabelcampAPIException($parsedBody->error_description, $status);
} elseif ($user_message) {
// It's a user error
throw new LabelcampAPIException($user_message, $status);
} else {
// Something went really wrong, we don't know what
throw new LabelcampAPIException('An unknown error occurred.', $status);
throw new LabelcampAPIException($errors[0]->detail, $status);
}

// Something went really wrong, we don't know what
throw new LabelcampAPIException('An unknown error occurred.', $status);
}

protected function parseError($summary = null) {
if (stripos($summary, 'access token expired') !== false) {
return 'The access token expired';
} elseif (stripos($summary, 'access token is invalid') !== false) {
return 'Invalid refresh token';
/**
* Handle server errors.
*
* @param string $body The raw, unparsed response body.
* @param int $status The HTTP status code, passed along to any exceptions thrown.
*
* @throws LabelcampAPIException
*
* @return void
*/
protected function handleServerError(string $body, int $status): void {
$parsedBody = json_decode($body);

$message = $parsedBody->message ?? null;

if ($message) {
throw new LabelcampAPIException('API error: ' . $message, $status);
}

throw new LabelcampAPIException('API error: Internal Server Error', $status);
}

/**
Expand Down Expand Up @@ -143,6 +150,9 @@ public function send(string $method, string $url, array $options): array {
} catch (ClientException $exception) {
$response = $exception->getResponse();
$this->handleResponseError($exception->getResponse()->getBody()->getContents(), $exception->getResponse()->getStatusCode());
} catch (ServerException $exception) {
$response = $exception->getResponse();
$this->handleServerError($exception->getResponse()->getBody()->getContents(), $exception->getResponse()->getStatusCode());
}

$body = $parsedBody = $response->getBody();
Expand Down

0 comments on commit 2a3334a

Please sign in to comment.