JSON:API should include validation constraint violations in log context

Created on 1 January 2023, over 1 year ago
Updated 24 May 2024, 23 days ago

Problem/Motivation

When \Drupal\jsonapi\Exception\UnprocessableHttpEntityExceptions are logged by \Drupal\Core\EventSubscriber\ExceptionLoggingSubscriber, none of the detail about the actual validation errors are captured in the log entry. This can make responding to and fixing these errors more difficult, e.g. when analyzing server logs.

Relates to πŸ“Œ Reduce logging severity/don't log expired tokens/401s Needs review in spirit, in so far as we add a custom exception there, and decorate the core exception logging subscriber to do special-casing.

Steps to reproduce

Submit an invalid field value during a PATCH to an entity and observe that the log entry contains none of the validation violations.

Proposed resolution

Add an exception logging subscriber that specifically handles these exceptions.

Remaining tasks

Code.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

TBD.

✨ Feature request
Status

Active

Version

11.0 πŸ”₯

Component
JSON APIΒ  β†’

Last updated 2 days ago

Created by

πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States bogdog400

    Let me just add that at this moment, some details about what's missing or malformed in a JSON packet would be very, very helpful. I'm pulling my hair out over some 422 errors that weren't there before I added a field.

    I will dig in a bit myself but I want to encourage others to post any quick solutions like a patch.

  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    I will dig in a bit myself but I want to encourage others to post any quick solutions like a patch.

    There is an open MR.

  • πŸ‡ΊπŸ‡ΈUnited States bogdog400

    I tried the patch but couldn't get it to work. Alas.

    Also, this is logging code wouldn't work for me either when I inserted it in core/modules/jsonapi/src/Exception/UnprocessableHttpEntityException.php

    
     \Drupal::logger('myerror')->warning('This is my error:'. print_r($violations) );
    

    But this did:

     \Drupal::logger('myerrror')->warning('This is my error:'. $this->$violations->getFieldNames() );
    

    It didn't generate the kind of log that I wanted, though. But the log message said that "getFieldNames" didn't exist and then it included a nice print out of the rest of the object which helped me solve the problem.

    I'm fully aware that i wasn't doing the right thing with the PHP, but it was rather maddening and I'm leaving this as a potential help for others who experience it.

    I'm happy to test another patch if someone creates it. Thx.

  • πŸ‡ΊπŸ‡ΈUnited States bogdog400

    Here's a better version for logging. (Is there some good way to serialize PHP arrays? There must be a function.) There's usually just one violation. If there are more, you can add print options for them too.

      /**
       * Sets the constraint violations associated with this exception.
       *
       * @param \Drupal\Core\Entity\EntityConstraintViolationListInterface $violations
       *   The constraint violations.
       */
      public function setViolations(EntityConstraintViolationListInterface $violations) {
        $this->violations = $violations;
        $this->message = "Unprocesseable Entity: Violations:".$violations[0];
      }
    
    
  • Pipeline finished with Failed
    27 days ago
    Total: 242s
    #176775
  • Pipeline finished with Failed
    27 days ago
    Total: 181s
    #176779
  • Status changed to Needs review 27 days ago
  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    Back to NR for feedback per #9.

  • Pipeline finished with Failed
    27 days ago
    Total: 200s
    #176847
  • Status changed to Needs work 27 days ago
  • The Needs Review Queue Bot β†’ tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

  • Pipeline finished with Failed
    27 days ago
    Total: 654s
    #177304
  • Pipeline finished with Success
    26 days ago
    Total: 668s
    #177573
  • Status changed to Needs review 26 days ago
  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    Back to NR. Tests updated as well.

  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands

    I don't see tests? Am i missing something?

  • Status changed to Active 23 days ago
  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    I'm looking for feedback on the conversation at #8 and #9. I suppose Active is more accurate.

Production build 0.69.0 2024