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

CommentFileSizeAuthor
#5 3302299-nr-bot.txt2.54 KBneeds-review-queue-bot

Issue fork drupal-3302299

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

bradjones1 created an issue. See original summary.

bradjones1’s picture

Status: Active » Needs review
Issue tags: +Needs tests

I think this would also be fine to kill #3302301: Specifying `error.source.[line,file]` in HttpExceptionNormalizer violates json:api spec at same time since we are fixing up the source property.

Whipped this up quickly while working on our product but if this looks good to maintainers, easy to add one (which would also demonstrate how to add your own exception type in userland...)

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new2.54 KB

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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bradjones1’s picture

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).

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.