Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better exception mapping when using factories #41

Open
xabbuh opened this issue Sep 27, 2018 · 2 comments
Open

better exception mapping when using factories #41

xabbuh opened this issue Sep 27, 2018 · 2 comments
Labels
enhancement New feature or request
Milestone

Comments

@xabbuh
Copy link
Member

xabbuh commented Sep 27, 2018

Right now when using the factory option together with expected_exception, all caught exceptions are transformed into form errors that are mapped to the form holding the factory option.

A simple approach to better map errors to the real cause could be the following: When an exception is caught, check each child form for the expected_exception option. If one and only one child form declares to handle the caught exception too, map the error to this very form. In all other cases, the behaviour must not change.

@xabbuh xabbuh added the enhancement New feature or request label Sep 27, 2018
@xabbuh xabbuh added this to the 1.0 milestone Sep 27, 2018
@sstok
Copy link

sstok commented Sep 28, 2018

Is this is logic followed only at the root form, or in deeper forms as well? In the Park-Manager FormHandler I implemented it like this (root form only):

/**
 * Maps a an exception class to a form (by path).
 *
 * Note: The form-path is about the form, 'profile.username'
 * not 'profile.children.username'.
 *
 * @param string      $exceptionClass Fully qualified exception class-name
 * @param \Closure    $formatter      Closure callback to produce one or more FormErrors
 */
$handler->map('ExceptionClass', function (\Throwable $e) {
    // 'form-element(property-path)' eg. 'profile.username' or null for root

    return [
        'form-element(property-path)' => new FormError('Whoops', null, [], null, $e)
    ];

    // Root form
    return new FormError('Whoops', null, [], null, $e);
    return [null => new FormError('Whoops', null, [], null, $e)];

    // Allow multiple FormErrors as an exception might be wrapped
    return ['profile.username' => [new FormError('Whoops', null, [], null, $e)]];
});

You take an exception and then map it to a specific Form (using a semi property-path).


The technique of "this" bundle much more powerful 😦 🤷‍♂️ oh well, less maintenance for me 😸

@xabbuh
Copy link
Member Author

xabbuh commented Sep 29, 2018

@sstok Using the term "root form" in the initial description was not quite right. In fact I see it to work for every (compound) form type that uses the factory option. This can be the root form, but it will work the same with any form type built like that when being used in deeper levels of nested form structures.

Your code example looks very interesting by the way. It seems to me like it basically solves the idea I roughly sketched in #29.

@xabbuh xabbuh modified the milestones: 1.0, 0.8 Jun 29, 2020
@xabbuh xabbuh modified the milestones: 0.8, 0.9 Feb 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants