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

#181: Changed how errors are handled in leantime api calls #61

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

* Changed how errors are handled in Leantime api calls.
* Modified getSprintReportData to work with Leantime data
* Added project lead to client when syncing projects.
* Remove description from create invoice page.
Expand Down
37 changes: 15 additions & 22 deletions src/Service/LeantimeApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Service;

use App\Exception\ApiServiceException;
use App\Exception\EconomicsException;
use App\Interface\DataProviderServiceInterface;
use App\Model\Invoices\PagedResult;
use App\Model\Planning\Assignee;
Expand Down Expand Up @@ -532,6 +533,7 @@ private function getIssuesForProjectMilestone($projectId, $milestoneId): array
* Get from Leantime.
*
* @throws ApiServiceException
* @throws EconomicsException
*/
private function request(string $path, string $type, string $method, array $params = []): mixed
{
Expand All @@ -544,30 +546,21 @@ private function request(string $path, string $type, string $method, array $para
'params' => $params,
]]
);
$body = $response->getContent(false);
switch ($response->getStatusCode()) {
case 200:
if ($body) {
return json_decode($body, null, 512, JSON_THROW_ON_ERROR)->result;
}
break;
case 400:
case 401:
case 403:
case 409:
if ($body) {
$error = json_decode($body, null, 512, JSON_THROW_ON_ERROR);
if (!empty($error->errorMessages)) {
$msg = array_pop($error->errorMessages);
} else {
$msg = $error->errors->projectKey;
}
throw new ApiServiceException($msg);
}
break;

$body = $response->getContent();

if ($body) {
$data = json_decode($body, null, 512, JSON_THROW_ON_ERROR);

if (isset($data->error)) {
$message = $data->error->message;
throw new ApiServiceException($message, $data->error->code);
}

return $data->result;
}
} catch (\Throwable $e) {
throw new ApiServiceException($e->getMessage(), (int) $e->getCode(), $e);
throw new ApiServiceException('Error from Leantime API: '.$e->getMessage(), (int) $e->getCode(), $e);
}

return null;
Expand Down
Loading