Skip to content

Commit cd0ce06

Browse files
Improve exception handling and default behaviors in Unit class
Enhanced `Unit` class by refining exception handling for `loadCertificates`, `loadJsModules`, and `getCertificate` methods. Introduced default empty arrays and specific exceptions for better runtime resilience. Updated `UnitException` instantiation with error codes for richer context. Adjusted README badges for added links and improved visibility.
1 parent f96ad11 commit cd0ce06

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
<p align="center">
1010
<a href="https://packagist.org/packages/pavlusha311245/unit-php-sdk"><img src="https://img.shields.io/packagist/v/Pavlusha311245/unit-php-sdk?labelColor=%231e293b&color=%23702963&link=https%3A%2F%2Fpackagist.org%2Fpackages%2Fpavlusha311245%2Funit-php-sdk" alt="packagist link"></a>
11-
<a href="https://unit-sdk.pavlusha.me/"><img src="https://img.shields.io/website?url=https%3A%2F%2Funit-sdk.pavlusha.me%2F&label=documentation&link=https%3A%2F%2Funit-sdk.pavlusha.me%2F" alt="documentation link"></a>
1211
<img src="https://github.com/Pavlusha311245/nginx-unit-php-sdk/actions/workflows/deploy_codecov.yaml/badge.svg" alt="documentation link">
1312
<a href="https://codecov.io/gh/Pavlusha311245/nginx-unit-php-sdk" >
1413
<img src="https://codecov.io/gh/Pavlusha311245/nginx-unit-php-sdk/graph/badge.svg?token=FGTTDSJ7BX" alt="Codecov dabge"/>
1514
</a>
15+
<a href="https://deepwiki.com/Pavlusha311245/nginx-unit-php-sdk"><img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki"></a>
1616
</p>
1717

1818
## About

src/Http/UnitRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function send($uri, bool $associative = true, array $requestOptions = [])
9393
// $responseBody = $e->hasResponse() ? $e->getResponse()->getBody()->getContents() : null;
9494
// throw new UnitException($responseBody ? json_decode($responseBody)['error'] : '');
9595

96-
throw new UnitException($e->getMessage());
96+
throw new UnitException($e->getMessage(), $e->getCode());
9797
}
9898

9999
$rawData = json_decode($response->getBody()->getContents(), $associative);

src/Unit.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,17 @@ private function loadConfig(): void
108108
*/
109109
public function getCertificates(): array
110110
{
111-
$this->loadCertificates();
111+
try {
112+
$this->loadCertificates();
113+
114+
return $this->certificates;
115+
} catch (UnitException $exception) {
116+
if ($exception->getCode() == 404) {
117+
return [];
118+
}
112119

113-
return $this->certificates;
120+
throw new UnitException($exception->getMessage());
121+
}
114122
}
115123

116124
/**
@@ -221,10 +229,14 @@ public function setJsModules(array $js_modules): void
221229
*/
222230
private function loadJsModules(): void
223231
{
224-
$result = $this->request->send(ApiPathEnum::JS_MODULES->value);
232+
try {
233+
$result = $this->request->send(ApiPathEnum::JS_MODULES->value);
225234

226-
foreach ($result as $key => $value) {
227-
$this->js_modules[$key] = new JsModule($key, $value);
235+
foreach ($result as $key => $value) {
236+
$this->js_modules[$key] = new JsModule($key, $value);
237+
}
238+
} catch (UnitException $exception) {
239+
$this->js_modules = [];
228240
}
229241
}
230242

@@ -243,9 +255,17 @@ private function loadStatistics(): void
243255
*/
244256
public function getCertificate(string $certificateName): ?CertificateInterface
245257
{
246-
$this->loadCertificates();
258+
try {
259+
$this->loadCertificates();
247260

248-
return $this->certificates[$certificateName] ?? null;
261+
return $this->certificates[$certificateName];
262+
} catch (UnitException $exception) {
263+
if ($exception->getCode() == 404) {
264+
throw new UnitException('Certificate not found');
265+
}
266+
267+
throw new UnitException($exception->getMessage());
268+
}
249269
}
250270

251271
/**

0 commit comments

Comments
 (0)