Allow custom HTTP exception types to specify `source.[pointer|parameter]` in error response

Created on 7 August 2022, almost 2 years ago
Updated 9 October 2023, 8 months ago

Problem/Motivation

JSON:API module provides a number of customized normalizers for exception types which return a JSON pointer to the offending data in the request object. E.g., EntityAccessDeniedHttpExceptionNormalizer and UnprocessableHttpEntityExceptionNormalizer.

In my case, I wish to control access to particular Search API fields (I'm revealing them using jsonapi_search_api) and return a 402 Payment Required response to hint the client side to tell the user which fields are premium/access controlled.

Because of the class structures of Cacheable*HttpException and their underlying Symfony HTTP exception classes, there's no way to use one of the existing exception types b/c I can't override the status code, and they expect a certain shape of the underlying exception to create the JSON pointer data in the source error response property. Additionally, my source values are parameter, since they're specified in the query string.

I can create my own CacheablePaymentRequiredHttpException class, which is normalized by the basic HttpExceptionNormalizer, but it has no built-in method to include source data in the resulting normalization/json:api error response.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

✨ Feature request
Status

Needs work

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

Comments & Activities

Not all content is available!

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

  • The Needs Review Queue Bot β†’ tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

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

  • Open on Drupal.org β†’
    Environment: PHP 8.2 & MySQL 8
    last update 9 months ago
    Not currently mergeable.
  • last update 9 months ago
    Custom Commands Failed
  • last update 9 months ago
    Custom Commands Failed
  • last update 8 months ago
    30,383 pass, 2 fail
  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    Error is a bit hidden in the GitLab CI output, but here it is:

    
    ---- Drupal\Tests\jsonapi\Unit\Normalizer\HttpExceptionNormalizerTest ----
    
    
    Status    Group      Filename          Line Function                            
    --------------------------------------------------------------------------------
    [33mException Other      phpunit-365.xml      0 Drupal\Tests\jsonapi\Unit\Normalize
    [0m    PHPUnit Test failed to complete; Error: PHPUnit 9.6.8 by Sebastian Bergmann
        and contributors.
        
        Testing Drupal\Tests\jsonapi\Unit\Normalizer\HttpExceptionNormalizerTest
        E                                                                   1 / 1
        (100%)
        
        Time: 00:00.114, Memory: 6.00 MB
        
        There was 1 error:
        
        1)
        Drupal\Tests\jsonapi\Unit\Normalizer\HttpExceptionNormalizerTest::testNormalize
        Undefined array key "source"
        
        /builds/issue/drupal-3302299/core/modules/jsonapi/tests/src/Unit/Normalizer/HttpExceptionNormalizerTest.php:45
        /builds/issue/drupal-3302299/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
        /builds/issue/drupal-3302299/vendor/phpunit/phpunit/src/Framework/TestSuite.php:684
        /builds/issue/drupal-3302299/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:651
        /builds/issue/drupal-3302299/vendor/phpunit/phpunit/src/TextUI/Command.php:144
        /builds/issue/drupal-3302299/vendor/phpunit/phpunit/src/TextUI/Command.php:97
        
        ERRORS!
        Tests: 1, Assertions: 1, Errors: 1.
    [31mFail      run-tests. Unknown              0 Unknown                            
    [0m    FATAL Drupal\Tests\jsonapi\Unit\Normalizer\HttpExceptionNormalizerTest:
        test runner returned a non-zero error code (2).
Production build 0.69.0 2024