Skip to content

Commit a648715

Browse files
come-ncAndyScherzinger
authored andcommitted
fix: Use HintException instead of InvalidArgumentException
To carry translated error messages intended for the end user, HintException is the correct class. Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent 21e5f6e commit a648715

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

apps/provisioning_api/lib/Controller/VerificationController.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace OCA\Provisioning_API\Controller;
1111

12-
use InvalidArgumentException;
1312
use OC\Security\Crypto;
1413
use OCP\Accounts\IAccountManager;
1514
use OCP\AppFramework\Controller;
@@ -18,6 +17,7 @@
1817
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
1918
use OCP\AppFramework\Http\Attribute\OpenAPI;
2019
use OCP\AppFramework\Http\TemplateResponse;
20+
use OCP\HintException;
2121
use OCP\IL10N;
2222
use OCP\IRequest;
2323
use OCP\IUserManager;
@@ -54,13 +54,16 @@ public function showVerifyMail(string $token, string $userId, string $key): Temp
5454
try {
5555
if ($this->userSession->getUser()?->getUID() !== $userId) {
5656
// not a public page, hence getUser() must return an IUser
57-
throw new InvalidArgumentException($this->l10n->t('Logged in account is not mail address owner'));
57+
throw new HintException(
58+
'Logged in account is not mail address owner',
59+
$this->l10n->t('Logged in account is not mail address owner'),
60+
);
5861
}
5962
$email = $this->crypto->decrypt($key);
60-
} catch (\Exception $e) {
63+
} catch (HintException $e) {
6164
return new TemplateResponse(
6265
'core', 'error', [
63-
'errors' => [['error' => $e->getMessage()]]
66+
'errors' => [['error' => $e->getHint()]]
6467
], TemplateResponse::RENDER_AS_GUEST);
6568
}
6669

@@ -81,7 +84,10 @@ public function verifyMail(string $token, string $userId, string $key): Template
8184
$throttle = false;
8285
try {
8386
if ($this->userSession->getUser()?->getUID() !== $userId) {
84-
throw new InvalidArgumentException($this->l10n->t('Logged in account is not mail address owner'));
87+
throw new HintException(
88+
'Logged in account is not mail address owner',
89+
$this->l10n->t('Logged in account is not mail address owner'),
90+
);
8591
}
8692
$email = $this->crypto->decrypt($key);
8793
$ref = \substr(hash('sha256', $email), 0, 8);
@@ -94,7 +100,10 @@ public function verifyMail(string $token, string $userId, string $key): Template
94100
->getPropertyByValue($email);
95101

96102
if ($emailProperty === null) {
97-
throw new InvalidArgumentException($this->l10n->t('Email was already removed from account and cannot be confirmed anymore.'));
103+
throw new HintException(
104+
'Email was already removed from account and cannot be confirmed anymore.',
105+
$this->l10n->t('Email was already removed from account and cannot be confirmed anymore.'),
106+
);
98107
}
99108
$emailProperty->setLocallyVerified(IAccountManager::VERIFIED);
100109
$this->accountManager->updateAccount($userAccount);
@@ -106,8 +115,8 @@ public function verifyMail(string $token, string $userId, string $key): Template
106115
$throttle = true;
107116
$error = $this->l10n->t('Could not verify mail because the token is invalid.');
108117
}
109-
} catch (InvalidArgumentException $e) {
110-
$error = $e->getMessage();
118+
} catch (HintException $e) {
119+
$error = $e->getHint();
111120
} catch (\Exception $e) {
112121
$error = $this->l10n->t('An unexpected error occurred. Please contact your admin.');
113122
}

0 commit comments

Comments
 (0)