From 1b7ea59e8f8687d7dd70fff47fb26669ed55b4a7 Mon Sep 17 00:00:00 2001 From: Michal Lulco Date: Wed, 4 Nov 2026 01:13:50 +0100 Subject: [PATCH 1/2] Added custom error message for token authorization (#239) --- CHANGELOG.md | 3 +++ src/Authorization/TokenAuthorization.php | 8 +++++++- src/Exception/InvalidTokenException.php | 12 ++++++++++++ src/Misc/TokenRepositoryInterface.php | 8 +++++++- 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/Exception/InvalidTokenException.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 6acf0c595..12b7f8622 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,9 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip * change Inject from PHPDoc format to native PHP attribute * [Reverted] Remove BC addApi register method now requires ApiHandlerInterface as second parameter instead of string or service name +### Added +* Custom error message for token authorization + ## 3.2.0 ### Added 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 @@ + Date: Sat, 7 Nov 2026 10:49:46 +0100 Subject: [PATCH 2/2] Move custom authorization error to Unreleased --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12b7f8622..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 @@ -68,9 +71,6 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip * change Inject from PHPDoc format to native PHP attribute * [Reverted] Remove BC addApi register method now requires ApiHandlerInterface as second parameter instead of string or service name -### Added -* Custom error message for token authorization - ## 3.2.0 ### Added