Unhelpful error message with enumeration problem

Created on 19 July 2024, 6 months ago

Problem/Motivation

When a component defines a property with an enum value and is rendered with a value that is not an enum, the error message is unhelpful.

Drupal\Core\Render\Component\Exception\InvalidComponentException: [example] Does not have a value in the enumeration ["foo","bar"] in Drupal\Core\Theme\Component\ComponentValidator->validateProps() (line 223 of core/lib/Drupal/Core/Theme/Component/ComponentValidator.php).

How do you understand what component is causing the problem? There can be multiple components with the same enum values. For example, buttons, inputs, etc., with a variant or color enum property.

This is especially annoying when the enum value is removed from the component after some time and the value for it can be a twig variable and not a direct definition.

Steps to reproduce

$schema: https://git.drupalcode.org/project/drupal/-/raw/HEAD/core/assets/schemas/v1/metadata.schema.json
name: Example
props:
  type: object
  properties:
    example:
      title: Example
      enum:
        - foo
        - bar

{% include 'foo:foo' with { example: 'baz' } only %}

Drupal\Core\Render\Component\Exception\InvalidComponentException: [example] Does not have a value in the enumeration ["foo","bar"] in Drupal\Core\Theme\Component\ComponentValidator->validateProps() (line 223 of core/lib/Drupal/Core/Theme/Component/ComponentValidator.php).

Proposed resolution

Add the component ID to the error message. It will help a lot. At least, you can check all component usage.

✨ Feature request
Status

Active

Version

11.0 πŸ”₯

Component
single-directory componentsΒ  β†’

Last updated 17 minutes ago

Created by

πŸ‡·πŸ‡ΊRussia niklan Russia, Perm

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @niklan
  • Merge request !8846Add component ID to error message β†’ (Open) created by niklan
  • πŸ‡·πŸ‡ΊRussia niklan Russia, Perm

    Witht the merge request the error message will be:

    Drupal\Core\Render\Component\Exception\InvalidComponentException: [foo:foo] [example] Does not have a value in the enumeration ["foo","bar"] in Drupal\Core\Theme\Component\ComponentValidator->validateProps() (line 203 of core/lib/Drupal/Core/Theme/Component/ComponentValidator.php).

  • Pipeline finished with Canceled
    6 months ago
    Total: 463s
    #229081
  • πŸ‡·πŸ‡ΊRussia niklan Russia, Perm

    Also tried to add a value provided, which will help to find an issue even faster:

    Drupal\Core\Render\Component\Exception\InvalidComponentException: [foo:foo] [example] Does not have a value in the enumeration ["foo","bar"]. "baz" provided. in Drupal\Core\Theme\Component\ComponentValidator->validateProps() (line 203 of core/lib/Drupal/Core/Theme/Component/ComponentValidator.php).

  • Status changed to Needs review 6 months ago
  • πŸ‡·πŸ‡ΊRussia niklan Russia, Perm
  • Pipeline finished with Failed
    6 months ago
    #229090
  • Status changed to Needs work 6 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Appears to have test failures.

  • Pipeline finished with Success
    6 months ago
    Total: 504s
    #231367
  • Status changed to Needs review 6 months ago
  • πŸ‡·πŸ‡ΊRussia niklan Russia, Perm

    Added condition to append information about value only if it is provided.

  • πŸ‡·πŸ‡ΊRussia niklan Russia, Perm

    Also, I'm thinking, can this message be improved? I personally don't like the end result right now with [foo:foo] [example]. Maybe it is better to be something like: [provider:component/properties/property] β€” [foo:foo/properties/example]?

  • Pipeline finished with Success
    6 months ago
    Total: 581s
    #231726
  • πŸ‡«πŸ‡·France pdureau Paris

    Unhelpful error message with enumeration problem

    Thanks you for this proposal which is useful for all error messages, not only for enumeration problems.

    Maybe it is better to be something like: [provider:component/properties/property]

    No need for "/properties/" here. A 3 level structure seems to be enough: provider / component / prop.

    If we add slots to the mix later, they are in the same "provider + component" namespace as props (you can't name a slot and a prop the same), so it will still work.

  • πŸ‡·πŸ‡ΊRussia niklan Russia, Perm

    Changed message to be:

    [foo:foo/example] Does not have a value in the enumeration ["foo","bar"]. "baz" provided

  • Pipeline finished with Success
    6 months ago
    Total: 544s
    #245810
  • Status changed to RTBC 5 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Believe feedback has been addressed and improved message definitely appears to be an improvement

  • Status changed to Needs work 5 months ago
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    RTBC++ … except this is missing test coverage in \Drupal\Tests\Core\Theme\Component\ComponentValidatorTest πŸ˜…

  • πŸ‡·πŸ‡ΊRussia niklan Russia, Perm

    Basically, the validation itself is covered by \Drupal\Tests\Core\Theme\Component\ComponentValidatorTest::dataProviderValidatePropsInvalid and ctaTarget violates the allowed properties in the enum. What is not covered is the message itself.

    Should it be a dedicated test method for that case or should the \Drupal\Tests\Core\Theme\Component\ComponentValidatorTest::testValidatePropsInvalid assert the exception message as well?

    What is the best way to cover it?

  • Pipeline finished with Failed
    5 months ago
    Total: 747s
    #258243
  • First commit to issue fork.
  • Pipeline finished with Failed
    22 days ago
    Total: 127s
    #383166
  • πŸ‡¦πŸ‡ΊAustralia RichardGaunt Melbourne

    I've added test coverage for this change, also found a nit issue while testing the error messages.

    The newline separator was incorrect meaning `/n` was showing in the separated error messages - figured it was closely enough related to this issue to be included.

  • πŸ‡¦πŸ‡ΊAustralia RichardGaunt Melbourne
  • Pipeline finished with Failed
    22 days ago
    Total: 819s
    #383177
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Believe the redesign of core/tests/Drupal/Tests/Core/Theme/Component/ComponentValidatorTest.php is out of scope for adding test coverage for a message.

  • πŸ‡¦πŸ‡ΊAustralia RichardGaunt Melbourne

    @smustgrave reverted to array structure for dataprovider.

  • πŸ‡¦πŸ‡ΊAustralia RichardGaunt Melbourne
  • Pipeline finished with Success
    16 days ago
    Total: 3287s
    #389074
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thanks! Re-ran the unit failure and it was random.

Production build 0.71.5 2024