diff --git a/CHANGELOG.md b/CHANGELOG.md index 6acf0c595..9286084f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip ## [Unreleased][unreleased] +### Added +* Custom error message for token authorization + ## 3.6.0 - 2026-10-27 ### Security diff --git a/src/Authorization/TokenAuthorization.php b/src/Authorization/TokenAuthorization.php index f6a5e97bc..ed2f93091 100644 --- a/src/Authorization/TokenAuthorization.php +++ b/src/Authorization/TokenAuthorization.php @@ -4,6 +4,7 @@ namespace Tomaj\NetteApi\Authorization; +use Tomaj\NetteApi\Exception\InvalidTokenException; use Tomaj\NetteApi\Misc\IpDetectorInterface; use Tomaj\NetteApi\Misc\TokenRepositoryInterface; @@ -40,7 +41,12 @@ public function authorized(): bool return false; } - $result = $this->tokenRepository->validToken($token); + try { + $result = $this->tokenRepository->validToken($token); + } catch (InvalidTokenException $e) { + $this->errorMessage = $e->getMessage(); + return false; + } if (!$result) { $this->errorMessage = 'Token doesn\'t exists or isn\'t active'; return false; diff --git a/src/Exception/InvalidTokenException.php b/src/Exception/InvalidTokenException.php new file mode 100644 index 000000000..01f4903e3 --- /dev/null +++ b/src/Exception/InvalidTokenException.php @@ -0,0 +1,12 @@ +