swaggest / json-diff

JSON diff/rearrange/patch/pointer library for PHP
MIT License
220 stars 30 forks source link

Throw different exception for failed test op #49

Closed jakobw closed 2 years ago

jakobw commented 2 years ago

Hi! :wave:

This PR adds a new exception type, which allows users of this library to distinguish between apply() errors due to the patch not being applicable and errors due to failed test operations.

Our (@wmde's) use case is a REST API with a PATCH endpoint which should have a different response for these two types of errors. We would like to do the following (simplified):

try {
    $patch->apply( $original );
} catch ( PatchTestOperationFailedException $e ) {
    // response saying the provided test operation failed
} catch ( Exception $e ) {
    // response saying the patch cannot be applied to the resource
}

As far as I can tell the only way to distinguish the two at the moment is by the exception message, which is a bit awkward to handle in code.

codecov[bot] commented 2 years ago

Codecov Report

Merging #49 (6a9078c) into master (6d7c9d5) will not change coverage. The diff coverage is 100.00%.

:exclamation: Current head 6a9078c differs from pull request most recent head 9593b9e. Consider uploading reports for the commit 9593b9e to get more accurate results

@@           Coverage Diff           @@
##           master      #49   +/-   ##
=======================================
  Coverage   96.63%   96.63%           
=======================================
  Files          10       10           
  Lines         535      535           
=======================================
  Hits          517      517           
  Misses         18       18           
Impacted Files Coverage Δ
src/JsonPatch.php 100.00% <100.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

vearutop commented 2 years ago

Thank you, v3.9.0 tagged.

jakobw commented 2 years ago

That was quick, thanks!