Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Enhance invalid parameter exceptions #96

Closed

Conversation

nubs
Copy link
Contributor

@nubs nubs commented Apr 28, 2016

This fixes a couple minor issues in documentation/tests, adds some additional tests related to InvalidParameterExceptions, and most importantly adds some new functionality to these exceptions.

A unique exception code is added to each of the thrown InvalidParameterExceptions in the Parameters class. This code is returned in the JSON API errors response built by the InvalidParameterExceptionHandler.

Also, the "source" of the error is exposed when available by setting which parameter was invalid on the exception.

An example JSON API error response from the included exception handler before this change:

{
  "errors": [
    {}
  ]
}

An example JSON API error response after this change:

{
  "errors": [
    {
      "code": 1,
      "source": {"parameter": "include"}
    }
  ]
}

Thanks for this library! It's been a joy to work with.

This code will never throw an InvalidParameterException, so document
that it does.
Because getOffset throws an exception, the assertEquals was never even
being run.  Removing it makes things clearer that only an exception is
expected.
There were no tests for the code paths in getInclude and getSort that
would throw an InvalidParameterException.  These tests hit that code
path to improve code coverage.
By giving these unique exceptions a unique code, it's possible they can
be caught and handled separately.
The exception codes can be useful for referencing the specific error
that was thrown.
@nubs nubs force-pushed the enhance-invalid-parameter-exceptions branch from ec7da65 to 463ac85 Compare April 28, 2016 15:39
@@ -12,7 +12,32 @@
namespace Tobscure\JsonApi\Exception;

use Exception;
use Throwable;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's PHP 7 only, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, strange that the tests passed on older PHP versions - I'll remove the typehints though.

By including the JSON API source with InvalidParameterException's, the
caller is better informed on what action to take to fix their request.
The [JSON API docs](http://jsonapi.org/format/#error-objects) go into
some detail on how to specify the source of the error.
@nubs nubs force-pushed the enhance-invalid-parameter-exceptions branch from 463ac85 to f98653e Compare April 29, 2016 01:34
@tobyzerner
Copy link
Owner

Great work, thanks! Sorry it took so long to get it in :/

@nubs
Copy link
Contributor Author

nubs commented May 26, 2016

No worries. Thanks for taking a look!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants