Skip to content

Commit 9b8dd92

Browse files
authored
🥅 Improve Envoyer API error handling (#14)
2 parents 10d86ef + 5191564 commit 9b8dd92

File tree

3 files changed

+83
-26
lines changed

3 files changed

+83
-26
lines changed

‎src/EnvoyerApi.php

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
use Illuminate\Http\Client\PendingRequest;
66
use Illuminate\Support\Collection;
77
use Illuminate\Support\Facades\Http;
8-
use Log1x\EnvoyerDeploy\Exceptions\ApiKeyMissingException;
8+
use Log1x\EnvoyerDeploy\Exceptions\EnvoyerApiErrorException;
9+
use Log1x\EnvoyerDeploy\Exceptions\EnvoyerApiKeyMissingException;
910

1011
class EnvoyerApi
1112
{
@@ -34,6 +35,8 @@ public static function make(): EnvoyerApi
3435

3536
/**
3637
* Get the Envoyer HTTP client.
38+
*
39+
* @throws \Log1x\EnvoyerDeploy\Exceptions\EnvoyerApiKeyMissingException
3740
*/
3841
public function api(): PendingRequest
3942
{
@@ -44,6 +47,42 @@ public function api(): PendingRequest
4447
])->baseUrl($this->endpoint);
4548
}
4649

50+
/**
51+
* Get from the Envoyer API.
52+
*
53+
* @throws \Log1x\EnvoyerDeploy\Exceptions\EnvoyerApiErrorException
54+
*/
55+
public function get(string $uri): object
56+
{
57+
$response = $this->api()->get($uri);
58+
59+
if ($response->failed()) {
60+
throw new EnvoyerApiErrorException($response);
61+
}
62+
63+
return json_decode(
64+
$response->body()
65+
);
66+
}
67+
68+
/**
69+
* Post to the Envoyer API.
70+
*
71+
* @throws \Log1x\EnvoyerDeploy\Exceptions\EnvoyerApiErrorException
72+
*/
73+
public function post(string $uri): object
74+
{
75+
$response = $this->api()->post($uri);
76+
77+
if ($response->failed()) {
78+
throw new EnvoyerApiErrorException($response);
79+
}
80+
81+
return json_decode(
82+
$response->body()
83+
);
84+
}
85+
4786
/**
4887
* Get the Envoyer API endpoint.
4988
*/
@@ -75,7 +114,7 @@ public function apiKey(string $apiKey): EnvoyerApi
75114
/**
76115
* Get the Envoyer API key.
77116
*
78-
* @throws \Log1x\EnvoyerDeploy\Exceptions\ApiKeyMissingException
117+
* @throws \Log1x\EnvoyerDeploy\Exceptions\EnvoyerApiKeyMissingException
79118
*/
80119
public function getApiKey(): string
81120
{
@@ -86,7 +125,7 @@ public function getApiKey(): string
86125
$this->apiKey = config('envoyer.api_key', null);
87126

88127
if (empty($this->apiKey)) {
89-
throw new ApiKeyMissingException;
128+
throw new EnvoyerApiKeyMissingException;
90129
}
91130

92131
return $this->apiKey;
@@ -107,9 +146,7 @@ public function project(int $project): EnvoyerApi
107146
*/
108147
public function getProjects(): Collection
109148
{
110-
$projects = json_decode(
111-
$this->api()->get('/projects')->body()
112-
);
149+
$projects = $this->get('/projects');
113150

114151
return collect($projects->projects ?? []);
115152
}
@@ -119,9 +156,7 @@ public function getProjects(): Collection
119156
*/
120157
public function getProject(): ?object
121158
{
122-
$project = json_decode(
123-
$this->api()->get("/projects/{$this->project}")->body()
124-
);
159+
$project = $this->get("/projects/{$this->project}");
125160

126161
return $project->project ?? null;
127162
}
@@ -131,9 +166,7 @@ public function getProject(): ?object
131166
*/
132167
public function getDeployment(int $deployment): ?object
133168
{
134-
$deployment = json_decode(
135-
$this->api()->get("/projects/{$this->project}/deployments/{$deployment}")->body()
136-
);
169+
$deployment = $this->get("/projects/{$this->project}/deployments/{$deployment}");
137170

138171
return $deployment->deployment ?? null;
139172
}
@@ -143,9 +176,7 @@ public function getDeployment(int $deployment): ?object
143176
*/
144177
public function getDeployments(): Collection
145178
{
146-
$deployments = json_decode(
147-
$this->api()->get("/projects/{$this->project}/deployments")->body()
148-
);
179+
$deployments = $this->get("/projects/{$this->project}/deployments");
149180

150181
return collect($deployments->deployments ?? []);
151182
}
@@ -155,9 +186,7 @@ public function getDeployments(): Collection
155186
*/
156187
public function getActions(): Collection
157188
{
158-
$actions = json_decode(
159-
$this->api()->get('/actions')->body()
160-
);
189+
$actions = $this->get('/actions');
161190

162191
return collect($actions->actions ?? []);
163192
}
@@ -167,9 +196,7 @@ public function getActions(): Collection
167196
*/
168197
public function getHooks(): Collection
169198
{
170-
$hooks = json_decode(
171-
$this->api()->get("/projects/{$this->project}/hooks")->body()
172-
);
199+
$hooks = $this->get("/projects/{$this->project}/hooks");
173200

174201
return collect($hooks->hooks ?? []);
175202
}
@@ -179,9 +206,7 @@ public function getHooks(): Collection
179206
*/
180207
public function getFolders(): Collection
181208
{
182-
$folders = json_decode(
183-
$this->api()->get("/projects/{$this->project}/folders")->body()
184-
);
209+
$folders = $this->get("/projects/{$this->project}/folders");
185210

186211
return collect($folders->folders ?? []);
187212
}
@@ -191,6 +216,6 @@ public function getFolders(): Collection
191216
*/
192217
public function deploy(): void
193218
{
194-
$this->api()->post("/projects/{$this->project}/deployments");
219+
$this->post("/projects/{$this->project}/deployments");
195220
}
196221
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Log1x\EnvoyerDeploy\Exceptions;
4+
5+
use Exception;
6+
use Illuminate\Http\Client\Response;
7+
8+
class EnvoyerApiErrorException extends Exception
9+
{
10+
/**
11+
* The Envoyer error codes.
12+
*/
13+
protected array $errorCodes = [
14+
'400' => 'Valid data was given but the request has failed.',
15+
'401' => 'No valid API Key was given.',
16+
'404' => 'The request resource could not be found.',
17+
'422' => 'The payload has missing required parameters or invalid data was given.',
18+
'429' => 'Too many attempts.',
19+
'500' => 'Request failed due to an internal error in Envoyer.',
20+
'503' => 'Envoyer is offline for maintenance.',
21+
];
22+
23+
/**
24+
* Create a new exception instance.
25+
*
26+
* @return void
27+
*/
28+
public function __construct(Response $response)
29+
{
30+
parent::__construct($this->errorCodes[$response->status()] ?? $response->body());
31+
}
32+
}

‎src/Exceptions/ApiKeyMissingException.php renamed to ‎src/Exceptions/EnvoyerApiKeyMissingException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Exception;
66

7-
class ApiKeyMissingException extends Exception
7+
class EnvoyerApiKeyMissingException extends Exception
88
{
99
/**
1010
* Create a new exception instance.

0 commit comments

Comments
 (0)