Skip to content

Commit ed092a6

Browse files
committed
Fix and improve error handling
1 parent 73eff2f commit ed092a6

File tree

5 files changed

+50
-24
lines changed

5 files changed

+50
-24
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## 4.6.1 - 2025-08-29
8+
9+
### Fixed
10+
- Fixed and improved the handling of errors related to the plugin settings.
11+
712
## 4.6.0 - 2025-08-28
813

914
### Added

src/controllers/ApiController.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,29 +359,32 @@ public function actionRequestRun(): Response
359359
}
360360
}
361361

362-
private function handleError($e): Response
362+
private function handleError(\Exception $e): Response
363363
{
364-
return match (get_class($e)) {
365-
NotFoundException::class => $this->handleInvalidSiteIdError(),
366-
UnauthorizedException::class => $this->handleInvalidApiTokenError(),
364+
return match ($e->getCode()) {
365+
404 => $this->handleInvalidSiteIdError(),
366+
401, 403 => $this->handleInvalidApiTokenError(),
367367
default => $this->handleGenericError($e),
368368
};
369-
370369
}
371370

372371
private function handleInvalidApiTokenError(): Response
373372
{
374373
$this->response->setStatusCode(403);
375374
return $this->asJson([
376-
'error' => 'The Oh Dear API token is invalid.',
375+
'error' => Craft::t('ohdear', 'The Oh Dear API token "{apiToken}" is invalid.', [
376+
'apiToken' => OhDear::$plugin->getSettings()->getApiToken(),
377+
]),
377378
]);
378379
}
379380

380381
private function handleInvalidSiteIdError(): Response
381382
{
382383
$this->response->setStatusCode(404);
383384
return $this->asJson([
384-
'error' => 'The Oh Dear site could not be found. Please check your site ID.',
385+
'error' => Craft::t('ohdear', 'The Oh Dear site with the ID "{siteId}" could not be found.', [
386+
'siteId' => OhDear::$plugin->getSettings()->getSelectedSiteId()
387+
]),
385388
]);
386389
}
387390

src/models/Settings.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use craft\base\Model;
1515
use craft\behaviors\EnvAttributeParserBehavior;
1616
use craft\helpers\App;
17-
use Saloon\Exceptions\Request\Statuses\UnauthorizedException;
1817
use webhubworks\ohdear\OhDear;
1918

2019
/**
@@ -50,12 +49,14 @@ public function behaviors(): array
5049
}
5150

5251
/**
53-
* Determines if the plugin has an API key and
54-
* a selected site ID.
52+
* Determines if the plugin has an API key and a selected site ID.
5553
*/
5654
public function hasApiCredentials(): bool
5755
{
58-
return ! empty($this->apiToken) && ! empty($this->selectedSiteId) && ! empty($this->getApiToken()) && ! empty($this->getSelectedSiteId());
56+
return ! empty($this->apiToken) &&
57+
! empty($this->selectedSiteId) &&
58+
! empty($this->getApiToken()) &&
59+
! empty($this->getSelectedSiteId());
5960
}
6061

6162
public function getApiToken(): ?string
@@ -91,34 +92,43 @@ public function rules(): array
9192
[['apiToken', 'selectedSiteId'], 'default', 'value' => ''],
9293
['selectedSiteId', 'required', 'when' => function ($model) {
9394
return ! empty($model->apiToken);
94-
}],
95+
}, 'message' => Craft::t('ohdear', 'Please enter a valid site ID.')],
9596
[['apiToken'], 'validApiToken'],
9697
[['selectedSiteId'], 'validSelectedSiteId'],
9798
];
9899
}
99-
100-
public function validApiToken($attribute, $params): void
100+
101+
public function validApiToken($attribute, $params): bool
101102
{
102103
try {
103104
OhDear::$plugin->settingsService->getMe($this->{$attribute});
104-
} catch (UnauthorizedException $e) {
105-
$this->addError('apiToken', Craft::t('ohdear', 'API authentication failed.'));
105+
return true;
106+
106107
} catch (\Exception $e) {
107-
$this->addError('apiToken', $e->getMessage());
108+
match ($e->getCode()) {
109+
401 => $this->addError('apiToken', Craft::t('ohdear', 'API authentication failed.')),
110+
default => $this->addError('apiToken', $e->getMessage()),
111+
};
112+
return false;
108113
}
109114
}
110-
111-
public function validSelectedSiteId($attribute, $params): void
115+
116+
public function validSelectedSiteId($attribute, $params): bool
112117
{
113118
try {
114119
OhDear::$plugin->settingsService->getMonitor(
115120
$this->apiToken,
116121
(int)$this->{$attribute}
117122
);
118-
} catch (UnauthorizedException $e) {
119-
$this->addError('selectedSiteId', Craft::t('ohdear', 'API authentication failed.'));
123+
return true;
124+
120125
} catch (\Exception $e) {
121-
$this->addError('selectedSiteId', $e->getMessage());
126+
match ($e->getCode()) {
127+
404 => $this->addError('selectedSiteId', Craft::t('ohdear', 'A site with the ID "{id}" was not found.', ['id' => $this->{$attribute}])),
128+
401 => $this->addError('selectedSiteId', Craft::t('ohdear', 'API authentication failed.')),
129+
default => $this->addError('selectedSiteId', $e->getMessage()),
130+
};
131+
return false;
122132
}
123133
}
124134
}

src/translations/de/ohdear.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,8 @@
136136
'Your Health Check endpoint is protected by this secret. You can find the secret on Oh Dear\'s {link} page. If you change it, make sure to change it in Oh Dear as well.' => 'Der Health-Check-Endpunkt ist durch dieses Secret gesichert. Sie können das Secret in Oh Dears {link} finden. Falls Sie es ändern, stellen Sie sicher, dass Sie es auch in Oh Dear anpassen.',
137137
'Please provide a valid API token and site ID.' => 'Bitte geben Sie zuerst einen API-Token und eine Site-ID ein.',
138138
'API authentication failed.' => 'API-Authentifizierung fehlgeschlagen.',
139+
'A site with the ID "{id}" was not found.' => 'Eine Seite mit der ID {id} konnte nicht gefunden werden.',
140+
'Please enter a valid site ID.' => 'Bitte geben Sie eine gültige Site-ID ein.',
141+
'The Oh Dear API token "{apiToken}" is invalid.' => 'Der Oh Dear API-Token "{apiToken}" ist invalide.',
142+
'The Oh Dear site with the ID "{siteId}" could not be found.' => 'Die Oh Dear Seite mit der ID "{siteId}" konnte nicht gefunden werden.',
139143
];

src/widgets/OhDearWidget.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ public static function maxColspan(): ?int
5757
public function rules(): array
5858
{
5959
$rules = parent::rules();
60-
$rules = array_merge(
60+
61+
return array_merge(
6162
$rules,
6263
[
6364
['period', 'string'],
6465
['period', 'in', 'range' => ['hour', 'day', 'month']],
6566
]
6667
);
67-
return $rules;
6868
}
6969

7070
/**
@@ -95,6 +95,10 @@ public function getSettingsHtml(): ?string
9595
*/
9696
public function getBodyHtml(): ?string
9797
{
98+
if (! OhDear::$plugin->getSettings()->hasApiCredentials()) {
99+
return null;
100+
}
101+
98102
OhDearAsset::registerLangFile();
99103

100104
return Craft::$app->getView()->renderTemplate(

0 commit comments

Comments
 (0)