Overview

In the case of validation errors we return sets of violations and check their counts:

    [$tree, $props, $violations] = $this->convertClientToServer($layout, $model);
    if ($violations->count() > 0) {
      return new EntityConstraintViolationList($entity, iterator_to_array($violations));
    }
    [$props, $violations] = $this->clientModelToServerProps($tree, $model);
    if ($violations->count() > 0) {
      return [NULL, NULL, $violations];
    }

etc.

This feels a bit too much Go and not enough like PHP - we have exceptions, we can throw those in the case of error.

Proposed resolution

Add an exception wrapper around ConstraintViolationList and throw it when we detect violations, catch it in the correct places, and only handle the success case everywhere else.

User interface changes

None

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

longwave created an issue. See original summary.

longwave’s picture

In fact Symfony already provides Symfony\Component\Validator\Exception\ValidationFailedException which does exactly what we need.

tedbow’s picture

I didn't know about Symfony\Component\Validator\Exception\ValidationFailedException. seems like a good idea

tedbow’s picture

Status: Active » Needs work
longwave’s picture

Status: Needs work » Needs review

Didn't even use ValidationFailedException, we already have a specific ConstraintViolationException in this codebase!

Conflicts resolved, also added missing return types for #3495126: Move SDC specific logic out of ClientsideConversionTrait into component source plugins given I'm touching these lines anyway.

tedbow’s picture

Assigned: Unassigned » wim leers
Status: Needs review » Reviewed & tested by the community

Looks good to me. Assign to @wimleers because he or @f.mazeikis needs to approve this MR.

Didn't even use ValidationFailedException, we already have a specific ConstraintViolationException in this codebase!

don't have to do this in this issue but do we actually need `ConstraintViolationException`? couldn't we just useSymfony\Component\Validator\Exception\ValidationFailedException?

wim leers’s picture

Assigned: wim leers » longwave
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs followup

Found this via @longwave's #3495126-4: Move SDC specific logic out of ClientsideConversionTrait into component source plugins.

Like @tedbow in #4, I didn't know about Symfony's ValidationFailedException yet either — thanks @longwave in #2! 😄 But then in #6, @longwave decided to not use it because we already have …

… so I second @tedbow's questioning XB's own final class ConstraintViolationException extends \Exception.

Review

See MR. I spotted 2 bugs.

That's why I didn't create the novice follow-up issue that @larowlan requested at https://git.drupalcode.org/project/experience_builder/-/merge_requests/5...

longwave’s picture

Status: Needs work » Needs review

I started looking at converting ConstraintViolationException to ViolationFailedException but then realised that we can't do the followup if we are using the Symfony class directly? I think it's fine to have our own exception here instead of Symfony's, especially now we are catching it in API responses it's probably better to differentiate I think.

tedbow’s picture

re #10, Yeah I agree the benefit of having something like `violationWithPropertyPathReplacePrefix` on ConstraintViolationException seem more having our own exception class

longwave’s picture

Issue tags: -Needs followup

Added the remapping code here, no need for the followup.

wim leers’s picture

Status: Needs review » Reviewed & tested by the community

RTBC'd — with @longwave's latest commits, this now looks like a magnificent improvement!

Only a single request for either a follow-up or a refinement, to bring that gloriously more readable pattern to one more place: https://git.drupalcode.org/project/experience_builder/-/merge_requests/5...

Up to @longwave to decide whether to do it here or to create a follow-up and merge this right now 😄

longwave’s picture

Opening a followup for #13 because ApiPublishAllControllerTest needs converting from a kernel to a functional test in order for the exception subscriber to kick in properly, and that's turning out trickier than I expected.

  • longwave committed a93a5e6b on 0.x
    Issue #3497203 by longwave, tedbow, wim leers, larowlan: Throw...
longwave’s picture

Assigned: longwave » Unassigned
Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.