Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1016 from spiral/feature/filter
[spiral/filters] Add error handling when performing casting
- Loading branch information
Showing
11 changed files
with
185 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Filters\Attribute; | ||
|
||
use Spiral\Attributes\NamedArgumentConstructor; | ||
use Spiral\Filters\Exception\SetterException; | ||
|
||
#[\Attribute(\Attribute::TARGET_PROPERTY), NamedArgumentConstructor] | ||
class CastingErrorMessage | ||
{ | ||
protected ?\Closure $callback = null; | ||
|
||
/** | ||
* @param callable(SetterException $exception, mixed $value): string $callback | ||
*/ | ||
public function __construct( | ||
protected ?string $message = null, | ||
?callable $callback = null | ||
) { | ||
if ($callback !== null) { | ||
$this->callback = $callback(...); | ||
} | ||
} | ||
|
||
public function getMessage(SetterException $exception, mixed $value = null): ?string | ||
{ | ||
if ($this->callback instanceof \Closure) { | ||
return ($this->callback)($exception, $value); | ||
} | ||
|
||
return $this->message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Tests\Framework\Filter\Model; | ||
|
||
use Spiral\App\Request\CastingErrorMessages; | ||
use Spiral\Filters\Exception\ValidationException; | ||
use Spiral\Tests\Framework\Filter\FilterTestCase; | ||
|
||
final class CastingErrorMessagesTest extends FilterTestCase | ||
{ | ||
public function testValidationMessages(): void | ||
{ | ||
try { | ||
$this->getFilter(CastingErrorMessages::class, [ | ||
'uuid' => 'foo', | ||
'uuidWithValidationMessage' => 'foo', | ||
'uuidWithValidationMessageCallback' => 'foo', | ||
]); | ||
} catch (ValidationException $e) { | ||
$this->assertSame([ | ||
'uuid' => 'Unable to set UUID value. Invalid UUID string: foo', | ||
'uuidWithValidationMessage' => 'Invalid UUID', | ||
'uuidWithValidationMessageCallback' => 'Invalid UUID: foo. Error: Unable to set UUID value. Invalid UUID string: foo', | ||
], $e->errors); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\App\Request; | ||
|
||
use Ramsey\Uuid\UuidInterface; | ||
use Spiral\Filters\Attribute\Input\Post; | ||
use Spiral\Filters\Attribute\CastingErrorMessage; | ||
use Spiral\Filters\Model\Filter; | ||
|
||
final class CastingErrorMessages extends Filter | ||
{ | ||
#[Post] | ||
public UuidInterface $uuid; | ||
|
||
#[Post] | ||
#[CastingErrorMessage('Invalid UUID')] | ||
public UuidInterface $uuidWithValidationMessage; | ||
|
||
#[Post] | ||
#[CastingErrorMessage(callback: [self::class, 'validationMessageCallback'])] | ||
public UuidInterface $uuidWithValidationMessageCallback; | ||
|
||
public static function validationMessageCallback(\Throwable $e, mixed $value): string | ||
{ | ||
return \sprintf('Invalid UUID: %s. Error: %s', $value, $e->getMessage()); | ||
} | ||
} |