Skip to content

Commit

Permalink
#181: Changed how errors are handled in leantime api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
tuj committed Jan 9, 2024
1 parent 491ebd7 commit 935fd01
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
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, (int) $data->error->code ?? 0);

Check failure on line 557 in src/Service/LeantimeApiService.php

View workflow job for this annotation

GitHub Actions / Psalm static analysis (8.1)

RedundantCondition

src/Service/LeantimeApiService.php:557:61: RedundantCondition: Type int for $<tmp coalesce var>21144 is never null (see https://psalm.dev/122)

Check failure on line 557 in src/Service/LeantimeApiService.php

View workflow job for this annotation

GitHub Actions / Psalm static analysis (8.1)

TypeDoesNotContainNull

src/Service/LeantimeApiService.php:557:89: TypeDoesNotContainNull: Cannot resolve types for $<tmp coalesce var>21144 - int does not contain null (see https://psalm.dev/090)
}

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

0 comments on commit 935fd01

Please sign in to comment.