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

Error handling updated #9

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading