From 9e7e4c23d0641e81f389ec35fd468a9ea0957e58 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Mon, 21 Aug 2023 11:31:31 -0400 Subject: [PATCH] Add guard name to exceptions (#2481) * Add guard_name to DoesNotExist exceptions It's much easier to troubleshoot guard-related issues when the guard is included in the exception. --- src/Exceptions/PermissionDoesNotExist.php | 11 ++++++++--- src/Exceptions/RoleDoesNotExist.php | 12 ++++++++---- src/Models/Role.php | 4 ++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Exceptions/PermissionDoesNotExist.php b/src/Exceptions/PermissionDoesNotExist.php index e6d7c101e..b451ace6b 100644 --- a/src/Exceptions/PermissionDoesNotExist.php +++ b/src/Exceptions/PermissionDoesNotExist.php @@ -6,13 +6,18 @@ class PermissionDoesNotExist extends InvalidArgumentException { - public static function create(string $permissionName, string $guardName = '') + public static function create(string $permissionName, ?string $guardName) { return new static("There is no permission named `{$permissionName}` for guard `{$guardName}`."); } - public static function withId(int $permissionId, string $guardName = '') + /** + * @param int|string $permissionId + * @param string $guardName + * @return static + */ + public static function withId($permissionId, ?string $guardName) { - return new static("There is no [permission] with id `{$permissionId}` for guard `{$guardName}`."); + return new static("There is no [permission] with ID `{$permissionId}` for guard `{$guardName}`."); } } diff --git a/src/Exceptions/RoleDoesNotExist.php b/src/Exceptions/RoleDoesNotExist.php index cee34e146..a4871c665 100644 --- a/src/Exceptions/RoleDoesNotExist.php +++ b/src/Exceptions/RoleDoesNotExist.php @@ -6,13 +6,17 @@ class RoleDoesNotExist extends InvalidArgumentException { - public static function named(string $roleName) + public static function named(string $roleName, ?string $guardName) { - return new static("There is no role named `{$roleName}`."); + return new static("There is no role named `{$roleName}` for guard `{$guardName}`."); } - public static function withId(int $roleId) + /** + * @param int|string $roleId + * @return static + */ + public static function withId($roleId, ?string $guardName) { - return new static("There is no role with id `{$roleId}`."); + return new static("There is no role with ID `{$roleId}` for guard `{$guardName}`."); } } diff --git a/src/Models/Role.php b/src/Models/Role.php index 025147864..d96102a2f 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -103,7 +103,7 @@ public static function findByName(string $name, $guardName = null): RoleContract $role = static::findByParam(['name' => $name, 'guard_name' => $guardName]); if (! $role) { - throw RoleDoesNotExist::named($name); + throw RoleDoesNotExist::named($name, $guardName); } return $role; @@ -123,7 +123,7 @@ public static function findById($id, $guardName = null): RoleContract $role = static::findByParam([(new static())->getKeyName() => $id, 'guard_name' => $guardName]); if (! $role) { - throw RoleDoesNotExist::withId($id); + throw RoleDoesNotExist::withId($id, $guardName); } return $role;