Skip to content

Commit 3be71b9

Browse files
committed
simplify errors
1 parent aa7defc commit 3be71b9

File tree

1 file changed

+15
-48
lines changed

1 file changed

+15
-48
lines changed

app/SourceControlProviders/BitbucketV2.php

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -91,51 +91,21 @@ private function getAccessTokenWithClientCredentials(string $key, string $secret
9191
}
9292

9393
$errorBody = $response->json();
94-
$errorDescription = $errorBody['error_description'] ?? '';
95-
96-
// Check if the error is about public consumer
97-
if (str_contains($errorDescription, 'public') || str_contains($errorDescription, 'Cannot use client_credentials')) {
98-
Log::error('Bitbucket OAuth consumer is marked as public. It must be marked as private.', [
99-
'status' => $response->status(),
100-
'error' => $errorBody['error'] ?? 'unknown',
101-
'error_description' => $errorDescription,
102-
'instructions' => 'Go to Bitbucket Workspace Settings > OAuth consumers > Edit your consumer > Check "This is a private consumer" > Save',
103-
]);
104-
105-
throw new Exception('Your Bitbucket OAuth consumer is marked as "public" but must be marked as "private consumer" to use client credentials grant. Please edit your OAuth consumer in Bitbucket settings and check the "This is a private consumer" option.');
106-
}
107-
108-
// Check if the error is about missing callback URL
109-
if (str_contains($errorDescription, 'callback') || str_contains($errorDescription, 'callback uri')) {
110-
Log::error('Bitbucket OAuth consumer is missing callback URL', [
111-
'status' => $response->status(),
112-
'error' => $errorBody['error'] ?? 'unknown',
113-
'error_description' => $errorDescription,
114-
'instructions' => 'Go to Bitbucket Workspace Settings > OAuth consumers > Edit your consumer > Set a Callback URL (e.g., https://your-domain.com/callback) > Save',
115-
]);
116-
117-
throw new Exception('Your Bitbucket OAuth consumer is missing a callback URL. Please edit your OAuth consumer in Bitbucket settings and set a Callback URL (any valid URL will work, e.g., https://example.com/callback).');
118-
}
94+
$errorMessage = $errorBody['error_description'] ?? $errorBody['error'] ?? $errorBody['message'] ?? $response->body();
11995

12096
Log::error('Failed to get Bitbucket access token with client credentials', [
12197
'status' => $response->status(),
12298
'body' => $response->body(),
123-
'error' => $errorBody['error'] ?? 'unknown',
124-
'error_description' => $errorDescription,
99+
'error' => $errorMessage,
125100
]);
126101

127-
throw new Exception('Failed to obtain Bitbucket access token. Error: '.$errorDescription);
102+
throw new Exception($errorMessage);
128103
} catch (Exception $e) {
129-
// Re-throw configuration errors so they can be displayed to the user
130-
if (str_contains($e->getMessage(), 'private consumer') || str_contains($e->getMessage(), 'callback URL')) {
131-
throw $e;
132-
}
133-
134104
Log::error('Error getting Bitbucket access token with client credentials', [
135105
'error' => $e->getMessage(),
136106
]);
137107

138-
throw new Exception('Failed to obtain Bitbucket access token: '.$e->getMessage());
108+
throw $e;
139109
}
140110
}
141111

@@ -153,24 +123,15 @@ public function connect(): bool
153123
}
154124

155125
$errorBody = $res->json();
156-
$errorMessage = $errorBody['error_description'] ?? $errorBody['error'] ?? $res->body();
126+
$errorMessage = $errorBody['error_description'] ?? $errorBody['error'] ?? $errorBody['message'] ?? $res->body();
157127

158128
Log::error('Bitbucket V2 connection failed', [
159129
'status' => $res->status(),
160130
'body' => $res->body(),
161131
'error' => $errorMessage,
162132
]);
163133

164-
// Provide user-friendly error messages based on status code
165-
if ($res->status() === 401) {
166-
throw new Exception('Bitbucket authentication failed. Please verify your Key and Secret are correct.');
167-
}
168-
169-
if ($res->status() === 403) {
170-
throw new Exception('Bitbucket access denied. Please check that your OAuth consumer has the necessary permissions.');
171-
}
172-
173-
throw new Exception('Failed to connect to Bitbucket: '.$errorMessage);
134+
throw new Exception($errorMessage);
174135
}
175136

176137
/**
@@ -213,7 +174,9 @@ public function deployHook(string $repo, array $events, string $secret): array
213174
]);
214175

215176
if ($response->status() !== 201) {
216-
throw new FailedToDeployGitHook($response->body());
177+
$errorBody = $response->json();
178+
$errorMessage = $errorBody['error']['message'] ?? $errorBody['error_description'] ?? $errorBody['error'] ?? $errorBody['message'] ?? $response->body();
179+
throw new FailedToDeployGitHook($errorMessage);
217180
}
218181

219182
$hookData = $response->json();
@@ -245,7 +208,9 @@ public function destroyHook(string $repo, string $hookId): void
245208
}
246209

247210
if ($response->status() !== 204) {
248-
throw new FailedToDestroyGitHook($response->body());
211+
$errorBody = $response->json();
212+
$errorMessage = $errorBody['error']['message'] ?? $errorBody['error_description'] ?? $errorBody['error'] ?? $errorBody['message'] ?? $response->body();
213+
throw new FailedToDestroyGitHook($errorMessage);
249214
}
250215
}
251216

@@ -293,7 +258,9 @@ public function deployKey(string $title, string $repo, string $key): string
293258
);
294259

295260
if ($res->status() !== 200) {
296-
throw new FailedToDeployGitKey($res->json()['error']['message']);
261+
$errorBody = $res->json();
262+
$errorMessage = $errorBody['error']['message'] ?? $errorBody['error_description'] ?? $errorBody['error'] ?? $errorBody['message'] ?? $res->body();
263+
throw new FailedToDeployGitKey($errorMessage);
297264
}
298265

299266
return $res->json()['id'] ?? '';

0 commit comments

Comments
 (0)