From aca51490da46fde2ad61b2c3062b8435afd56e4b Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Fri, 18 Aug 2023 22:04:31 -0400 Subject: [PATCH 1/2] 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 | 13 +++++++++---- src/Models/Role.php | 4 ++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Exceptions/PermissionDoesNotExist.php b/src/Exceptions/PermissionDoesNotExist.php index e6d7c101e..ab760d267 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..ea0ceac14 100644 --- a/src/Exceptions/RoleDoesNotExist.php +++ b/src/Exceptions/RoleDoesNotExist.php @@ -6,13 +6,18 @@ 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 + * @param string|null $guardName + * @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; From a53855473b8bbe4443bc4aaa5bf14714fc5a39a8 Mon Sep 17 00:00:00 2001 From: drbyte Date: Sat, 19 Aug 2023 02:18:49 +0000 Subject: [PATCH 2/2] Fix styling --- src/Exceptions/PermissionDoesNotExist.php | 4 ++-- src/Exceptions/RoleDoesNotExist.php | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Exceptions/PermissionDoesNotExist.php b/src/Exceptions/PermissionDoesNotExist.php index ab760d267..b451ace6b 100644 --- a/src/Exceptions/PermissionDoesNotExist.php +++ b/src/Exceptions/PermissionDoesNotExist.php @@ -12,8 +12,8 @@ public static function create(string $permissionName, ?string $guardName) } /** - * @param int|string $permissionId - * @param string $guardName + * @param int|string $permissionId + * @param string $guardName * @return static */ public static function withId($permissionId, ?string $guardName) diff --git a/src/Exceptions/RoleDoesNotExist.php b/src/Exceptions/RoleDoesNotExist.php index ea0ceac14..a4871c665 100644 --- a/src/Exceptions/RoleDoesNotExist.php +++ b/src/Exceptions/RoleDoesNotExist.php @@ -12,8 +12,7 @@ public static function named(string $roleName, ?string $guardName) } /** - * @param int|string $roleId - * @param string|null $guardName + * @param int|string $roleId * @return static */ public static function withId($roleId, ?string $guardName)