spiral / framework

High-Performance PHP Framework
https://spiral.dev
MIT License
1.8k stars 87 forks source link

[spiral/filters] Add error handling when performing casting #1016

Closed msmakouz closed 10 months ago

msmakouz commented 10 months ago
Q A
Bugfix? ✔️
Breaks BC?
New feature? ✔️

What was changed

Added exception handling for errors occurring during value casting using Spiral\Filters\Model\Mapper\EnumCaster, Spiral\Filters\Model\Mapper\UuidCaster, and Spiral\Filters\Model\Mapper\DefaultCaster. Now, in the event of errors, a Spiral\Filters\Exception\SetterException will be thrown.

Introduced the Spiral\Filters\Attribute\CastingErrorMessage attribute, allowing the specification of an error message for enhanced error handling.

use Ramsey\Uuid\UuidInterface;
use Spiral\Filters\Attribute\Input\Post;
use Spiral\Filters\Attribute\CastingErrorMessage;
use Spiral\Filters\Model\Filter;

final class CreateUser extends Filter
{
    #[Post]
    public string $name;

    #[Post]
    #[CastingErrorMessage('Invalid UUID')]
    public UuidInterface $groupUuid;
}
codecov[bot] commented 10 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (6714d80) 89.17% compared to head (a9d1024) 89.21%. Report is 6 commits behind head on master.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1016 +/- ## ============================================ + Coverage 89.17% 89.21% +0.04% - Complexity 6124 6146 +22 ============================================ Files 808 810 +2 Lines 17307 17364 +57 ============================================ + Hits 15433 15492 +59 + Misses 1874 1872 -2 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

butschster commented 10 months ago

It works as expected!