Skip to content

Commit

Permalink
Add guard name to exceptions (#2481)
Browse files Browse the repository at this point in the history
* Add guard_name to DoesNotExist exceptions

It's much easier to troubleshoot guard-related issues when the guard is included in the exception.
  • Loading branch information
drbyte committed Aug 21, 2023
1 parent e915a89 commit 9e7e4c2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/Exceptions/PermissionDoesNotExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}`.");
}
}
12 changes: 8 additions & 4 deletions src/Exceptions/RoleDoesNotExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}`.");
}
}
4 changes: 2 additions & 2 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 9e7e4c2

Please sign in to comment.