Skip to content

Commit f19c61d

Browse files
authored
Merge pull request #114 from utopia-php/fix/github-token-exception-code
fix: pass HTTP status code as exception code
2 parents 598cbd2 + 96da02e commit f19c61d

7 files changed

Lines changed: 77 additions & 63 deletions

File tree

src/VCS/Adapter/Git/GitHub.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function createFile(string $owner, string $repositoryName, string $filepa
164164
$responseHeaders = $response['headers'] ?? [];
165165
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
166166
if ($responseHeadersStatusCode >= 400) {
167-
throw new Exception("Failed to create file {$filepath}: HTTP {$responseHeadersStatusCode}");
167+
throw new Exception("Failed to create file {$filepath}: HTTP {$responseHeadersStatusCode}", $responseHeadersStatusCode);
168168
}
169169

170170
return $response['body'] ?? [];
@@ -530,7 +530,7 @@ public function deleteRepository(string $owner, string $repositoryName): bool
530530
$responseHeaders = $response['headers'] ?? [];
531531
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
532532
if ($responseHeadersStatusCode >= 400) {
533-
throw new Exception("Deleting repository $repositoryName failed with status code $responseHeadersStatusCode");
533+
throw new Exception("Deleting repository $repositoryName failed with status code $responseHeadersStatusCode", $responseHeadersStatusCode);
534534
}
535535
return true;
536536
}
@@ -639,7 +639,7 @@ protected function generateAccessToken(string $privateKey, ?string $appId): void
639639
$statusCode = $response['headers']['status-code'] ?? 0;
640640
if (!array_key_exists('token', $responseBody)) {
641641
$safeBody = \is_array($responseBody) ? \json_encode(\array_intersect_key($responseBody, \array_flip(['message', 'documentation_url']))) : '';
642-
throw new Exception('Failed to retrieve access token from GitHub API. Status: ' . $statusCode . '. Response: ' . $safeBody);
642+
throw new Exception('Failed to retrieve access token from GitHub API. Status: ' . $statusCode . '. Response: ' . $safeBody, $statusCode);
643643
}
644644
$this->accessToken = $responseBody['token'] ?? '';
645645
}
@@ -852,21 +852,15 @@ public function getLatestCommit(string $owner, string $repositoryName, string $b
852852
if ($responseHeadersStatusCode === 404) {
853853
throw new RepositoryNotFound("Branch not found: {$branch}");
854854
}
855+
if ($responseHeadersStatusCode >= 400) {
856+
throw new Exception("Failed to get latest commit: HTTP {$responseHeadersStatusCode}", $responseHeadersStatusCode);
857+
}
855858

856859
$responseBody = $response['body'] ?? [];
857860
$responseBodyCommit = $responseBody['commit'] ?? [];
858861
$responseBodyCommitAuthor = $responseBodyCommit['author'] ?? [];
859862
$responseBodyAuthor = is_array($responseBody['author'] ?? null) ? $responseBody['author'] : [];
860863

861-
if (
862-
!array_key_exists('name', $responseBodyCommitAuthor) ||
863-
!array_key_exists('message', $responseBodyCommit) ||
864-
!array_key_exists('sha', $responseBody) ||
865-
!array_key_exists('html_url', $responseBody)
866-
) {
867-
throw new Exception("Latest commit response is missing required information.");
868-
}
869-
870864
return [
871865
'commitAuthor' => $responseBodyCommitAuthor['name'] ?? '',
872866
'commitMessage' => $responseBodyCommit['message'] ?? '',
@@ -972,7 +966,7 @@ public function createCheckRun(
972966

973967
$responseHeadersStatusCode = $response['headers']['status-code'] ?? 0;
974968
if ($responseHeadersStatusCode >= 400) {
975-
throw new Exception("Failed to create check run: HTTP $responseHeadersStatusCode");
969+
throw new Exception("Failed to create check run: HTTP $responseHeadersStatusCode", $responseHeadersStatusCode);
976970
}
977971

978972
return $response['body'] ?? [];
@@ -991,7 +985,7 @@ public function getCheckRun(string $owner, string $repositoryName, int $checkRun
991985

992986
$responseHeadersStatusCode = $response['headers']['status-code'] ?? 0;
993987
if ($responseHeadersStatusCode >= 400) {
994-
throw new Exception("Failed to get check run $checkRunId: HTTP $responseHeadersStatusCode");
988+
throw new Exception("Failed to get check run $checkRunId: HTTP $responseHeadersStatusCode", $responseHeadersStatusCode);
995989
}
996990

997991
return $response['body'] ?? [];
@@ -1068,7 +1062,7 @@ public function updateCheckRun(
10681062

10691063
$responseHeadersStatusCode = $response['headers']['status-code'] ?? 0;
10701064
if ($responseHeadersStatusCode >= 400) {
1071-
throw new Exception("Failed to update check run $checkRunId: HTTP $responseHeadersStatusCode");
1065+
throw new Exception("Failed to update check run $checkRunId: HTTP $responseHeadersStatusCode", $responseHeadersStatusCode);
10721066
}
10731067

10741068
return $response['body'] ?? [];

src/VCS/Adapter/Git/GitLab.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function createOrganization(string $orgName): string
7070
$responseHeaders = $response['headers'] ?? [];
7171
$statusCode = $responseHeaders['status-code'] ?? 0;
7272
if ($statusCode >= 400) {
73-
throw new Exception("Creating organization {$orgName} failed with status code {$statusCode}");
73+
throw new Exception("Creating organization {$orgName} failed with status code {$statusCode}", $statusCode);
7474
}
7575

7676
return ($responseBody['id'] ?? '') . ':' . ($responseBody['path'] ?? '');
@@ -116,7 +116,7 @@ public function createRepository(string $owner, string $repositoryName, bool $pr
116116
$responseHeaders = $response['headers'] ?? [];
117117
$statusCode = $responseHeaders['status-code'] ?? 0;
118118
if ($statusCode >= 400) {
119-
throw new Exception("Creating repository {$repositoryName} failed with status code {$statusCode}");
119+
throw new Exception("Creating repository {$repositoryName} failed with status code {$statusCode}", $statusCode);
120120
}
121121
$result = is_array($body) ? $body : [];
122122
$result['pushed_at'] = $result['last_activity_at'] ?? '';
@@ -134,7 +134,7 @@ public function deleteRepository(string $owner, string $repositoryName): bool
134134
$responseHeaders = $response['headers'] ?? [];
135135
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
136136
if ($responseHeadersStatusCode >= 400) {
137-
throw new Exception("Deleting repository {$repositoryName} failed with status code {$responseHeadersStatusCode}");
137+
throw new Exception("Deleting repository {$repositoryName} failed with status code {$responseHeadersStatusCode}", $responseHeadersStatusCode);
138138
}
139139

140140
return true;
@@ -393,7 +393,7 @@ public function createFile(string $owner, string $repositoryName, string $filepa
393393
$responseHeaders = $response['headers'] ?? [];
394394
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
395395
if ($responseHeadersStatusCode >= 400) {
396-
throw new Exception("Failed to create file {$filepath}: HTTP {$responseHeadersStatusCode}");
396+
throw new Exception("Failed to create file {$filepath}: HTTP {$responseHeadersStatusCode}", $responseHeadersStatusCode);
397397
}
398398

399399
return $response['body'] ?? [];
@@ -413,7 +413,7 @@ public function createBranch(string $owner, string $repositoryName, string $newB
413413
$responseHeaders = $response['headers'] ?? [];
414414
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
415415
if ($responseHeadersStatusCode >= 400) {
416-
throw new Exception("Failed to create branch {$newBranchName}: HTTP {$responseHeadersStatusCode}");
416+
throw new Exception("Failed to create branch {$newBranchName}: HTTP {$responseHeadersStatusCode}", $responseHeadersStatusCode);
417417
}
418418

419419
return $response['body'] ?? [];
@@ -437,7 +437,7 @@ public function createPullRequest(string $owner, string $repositoryName, string
437437
$responseHeaders = $response['headers'] ?? [];
438438
$statusCode = $responseHeaders['status-code'] ?? 0;
439439
if ($statusCode >= 400) {
440-
throw new Exception("Failed to create merge request: HTTP {$statusCode}");
440+
throw new Exception("Failed to create merge request: HTTP {$statusCode}", $statusCode);
441441
}
442442

443443
return $response['body'] ?? [];
@@ -463,7 +463,7 @@ public function createWebhook(string $owner, string $repositoryName, string $url
463463
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
464464
if ($responseHeadersStatusCode >= 400) {
465465
$body = $response['body'] ?? [];
466-
throw new Exception("Failed to create webhook: HTTP {$responseHeadersStatusCode} - " . json_encode($body));
466+
throw new Exception("Failed to create webhook: HTTP {$responseHeadersStatusCode} - " . json_encode($body), $responseHeadersStatusCode);
467467
}
468468

469469
$responseBody = $response['body'] ?? [];
@@ -481,7 +481,7 @@ public function createComment(string $owner, string $repositoryName, int $pullRe
481481
$responseHeaders = $response['headers'] ?? [];
482482
$statusCode = $responseHeaders['status-code'] ?? 0;
483483
if ($statusCode >= 400) {
484-
throw new Exception("Failed to create comment: HTTP {$statusCode}");
484+
throw new Exception("Failed to create comment: HTTP {$statusCode}", $statusCode);
485485
}
486486

487487
$responseBody = $response['body'] ?? [];
@@ -525,7 +525,7 @@ public function updateComment(string $owner, string $repositoryName, string $com
525525

526526
$responseHeaders = $response['headers'] ?? [];
527527
if (($responseHeaders['status-code'] ?? 0) !== 200) {
528-
throw new Exception("Failed to update comment: HTTP " . ($responseHeaders['status-code'] ?? 0));
528+
throw new Exception("Failed to update comment: HTTP " . ($responseHeaders['status-code'] ?? 0), $responseHeaders['status-code'] ?? 0);
529529
}
530530

531531
return $commentId;
@@ -540,7 +540,7 @@ public function getUser(string $username): array
540540
$responseHeaders = $response['headers'] ?? [];
541541
$statusCode = $responseHeaders['status-code'] ?? 0;
542542
if ($statusCode >= 400) {
543-
throw new Exception("Failed to get user: HTTP {$statusCode}");
543+
throw new Exception("Failed to get user: HTTP {$statusCode}", $statusCode);
544544
}
545545

546546
$body = $response['body'] ?? [];
@@ -561,7 +561,7 @@ public function getOwnerName(string $installationId, ?int $repositoryId = null):
561561
$responseHeaders = $response['headers'] ?? [];
562562
$statusCode = $responseHeaders['status-code'] ?? 0;
563563
if ($statusCode >= 400) {
564-
throw new Exception("Failed to get owner name for repository {$repositoryId}: HTTP {$statusCode}");
564+
throw new Exception("Failed to get owner name for repository {$repositoryId}: HTTP {$statusCode}", $statusCode);
565565
}
566566
$responseBody = $response['body'] ?? [];
567567
$namespace = $responseBody['namespace'] ?? [];
@@ -573,7 +573,7 @@ public function getOwnerName(string $installationId, ?int $repositoryId = null):
573573
$responseHeaders = $response['headers'] ?? [];
574574
$statusCode = $responseHeaders['status-code'] ?? 0;
575575
if ($statusCode >= 400) {
576-
throw new Exception("Failed to get current user: HTTP {$statusCode}");
576+
throw new Exception("Failed to get current user: HTTP {$statusCode}", $statusCode);
577577
}
578578
$responseBody = $response['body'] ?? [];
579579
return $responseBody['username'] ?? '';
@@ -590,7 +590,7 @@ public function getPullRequest(string $owner, string $repositoryName, int $pullR
590590
$responseHeaders = $response['headers'] ?? [];
591591
$statusCode = $responseHeaders['status-code'] ?? 0;
592592
if ($statusCode >= 400) {
593-
throw new Exception("Failed to get merge request: HTTP {$statusCode}");
593+
throw new Exception("Failed to get merge request: HTTP {$statusCode}", $statusCode);
594594
}
595595

596596
$mr = $response['body'] ?? [];
@@ -642,7 +642,7 @@ public function getPullRequestFiles(string $owner, string $repositoryName, int $
642642
$responseHeaders = $response['headers'] ?? [];
643643
$statusCode = $responseHeaders['status-code'] ?? 0;
644644
if ($statusCode >= 400) {
645-
throw new Exception("Failed to get merge request files: HTTP {$statusCode}");
645+
throw new Exception("Failed to get merge request files: HTTP {$statusCode}", $statusCode);
646646
}
647647

648648
$files = $response['body'] ?? [];
@@ -676,7 +676,7 @@ public function getPullRequestFromBranch(string $owner, string $repositoryName,
676676
$responseHeaders = $response['headers'] ?? [];
677677
$statusCode = $responseHeaders['status-code'] ?? 0;
678678
if ($statusCode >= 400) {
679-
throw new Exception("Failed to list merge requests: HTTP {$statusCode}");
679+
throw new Exception("Failed to list merge requests: HTTP {$statusCode}", $statusCode);
680680
}
681681

682682
$body = $response['body'] ?? [];
@@ -765,7 +765,7 @@ public function getLatestCommit(string $owner, string $repositoryName, string $b
765765
$responseHeaders = $response['headers'] ?? [];
766766
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
767767
if ($responseHeadersStatusCode >= 400) {
768-
throw new Exception("Failed to get latest commit: HTTP {$responseHeadersStatusCode}");
768+
throw new Exception("Failed to get latest commit: HTTP {$responseHeadersStatusCode}", $responseHeadersStatusCode);
769769
}
770770

771771
$responseBody = $response['body'] ?? [];
@@ -823,7 +823,7 @@ public function updateCommitStatus(string $repositoryName, string $commitHash, s
823823
$responseHeaders = $response['headers'] ?? [];
824824
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
825825
if ($responseHeadersStatusCode >= 400) {
826-
throw new Exception("Failed to update commit status: HTTP {$responseHeadersStatusCode}");
826+
throw new Exception("Failed to update commit status: HTTP {$responseHeadersStatusCode}", $responseHeadersStatusCode);
827827
}
828828
}
829829

@@ -968,7 +968,7 @@ public function createTag(string $owner, string $repositoryName, string $tagName
968968
$responseHeaders = $response['headers'] ?? [];
969969
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
970970
if ($responseHeadersStatusCode >= 400) {
971-
throw new Exception("Failed to create tag {$tagName}: HTTP {$responseHeadersStatusCode}");
971+
throw new Exception("Failed to create tag {$tagName}: HTTP {$responseHeadersStatusCode}", $responseHeadersStatusCode);
972972
}
973973

974974
return $response['body'] ?? [];

0 commit comments

Comments
 (0)