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

Commit

Permalink
Enhance invalid parameter exceptions. closes #96
Browse files Browse the repository at this point in the history
  • Loading branch information
nubs authored and tobyzerner committed May 26, 2016
1 parent 0feda5d commit d9eccd9
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/Exception/Handler/InvalidParameterExceptionHandler.php
Expand Up @@ -32,6 +32,16 @@ public function handle(Exception $e)
$status = 400;
$error = [];

$code = $e->getCode();
if ($code) {
$error['code'] = $code;
}

$invalidParameter = $e->getInvalidParameter();
if ($invalidParameter) {
$error['source'] = ['parameter' => $invalidParameter];
}

return new ResponseBag($status, [$error]);
}
}
25 changes: 24 additions & 1 deletion src/Exception/InvalidParameterException.php
Expand Up @@ -15,5 +15,28 @@

class InvalidParameterException extends Exception
{
//
/**
* @var string The parameter that caused this exception.
*/
private $invalidParameter;

/**
* {@inheritdoc}
*
* @param string $invalidParameter The parameter that caused this exception.
*/
public function __construct($message = '', $code = 0, $previous = null, $invalidParameter = '')
{
parent::__construct($message, $code, $previous);

$this->invalidParameter = $invalidParameter;
}

/**
* @return string The parameter that caused this exception.
*/
public function getInvalidParameter()
{
return $this->invalidParameter;
}
}
16 changes: 13 additions & 3 deletions src/Parameters.php
Expand Up @@ -45,7 +45,12 @@ public function getInclude(array $available = [])
$invalid = array_diff($relationships, $available);

if (count($invalid)) {
throw new InvalidParameterException('Invalid includes ['.implode(',', $invalid).']');
throw new InvalidParameterException(
'Invalid includes ['.implode(',', $invalid).']',
1,
null,
'include'
);
}

return $relationships;
Expand All @@ -72,7 +77,7 @@ public function getOffset($perPage = null)
$offset = (int) $this->getPage('offset');

if ($offset < 0) {
throw new InvalidParameterException('page[offset] must be >=0');
throw new InvalidParameterException('page[offset] must be >=0', 2, null, 'page[offset]');
}

return $offset;
Expand Down Expand Up @@ -146,7 +151,12 @@ public function getSort(array $available = [])
$invalid = array_diff(array_keys($sort), $available);

if (count($invalid)) {
throw new InvalidParameterException('Invalid sort fields ['.implode(',', $invalid).']');
throw new InvalidParameterException(
'Invalid sort fields ['.implode(',', $invalid).']',
3,
null,
'sort'
);
}
}

Expand Down
44 changes: 44 additions & 0 deletions tests/Exception/Handler/InvalidParameterExceptionHandlerTest.php
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of JSON-API.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tobscure\Tests\Exception\Handler;

use Exception;
use Tobscure\JsonApi\Exception\Handler\InvalidParameterExceptionHandler;
use Tobscure\JsonApi\Exception\Handler\ResponseBag;
use Tobscure\JsonApi\Exception\InvalidParameterException;

class InvalidParameterExceptionHandlerTest extends \PHPUnit_Framework_TestCase
{
public function testHandlerCanManageInvalidParameterExceptions()
{
$handler = new InvalidParameterExceptionHandler();

$this->assertTrue($handler->manages(new InvalidParameterException));
}

public function testHandlerCanNotManageOtherExceptions()
{
$handler = new InvalidParameterExceptionHandler();

$this->assertFalse($handler->manages(new Exception));
}

public function testErrorHandling()
{
$handler = new InvalidParameterExceptionHandler();
$response = $handler->handle(new InvalidParameterException('error', 1, null, 'include'));

$this->assertInstanceOf(ResponseBag::class, $response);
$this->assertEquals(400, $response->getStatus());
$this->assertEquals([['code' => 1, 'source' => ['parameter' => 'include']]], $response->getErrors());
}
}
25 changes: 24 additions & 1 deletion tests/ParametersTest.php
Expand Up @@ -34,6 +34,17 @@ public function testGetIncludeReturnsEmptyArray()
$this->assertEquals([], $parameters->getInclude(['posts', 'images']));
}

/**
* @expectedException \Tobscure\JsonApi\Exception\InvalidParameterException
* @expectedExceptionCode 1
*/
public function testGetIncludeWithUnallowedField()
{
$parameters = new Parameters(['include' => 'posts,images']);

$parameters->getInclude(['posts']);
}

public function testGetSortReturnsArrayOfFieldToSortDirection()
{
$parameters = new Parameters(['sort' => 'firstname']);
Expand All @@ -55,6 +66,17 @@ public function testGetSortDefaultsToEmptyArray()
$this->assertEmpty($parameters->getSort());
}

/**
* @expectedException \Tobscure\JsonApi\Exception\InvalidParameterException
* @expectedExceptionCode 3
*/
public function testGetSortWithUnallowedField()
{
$parameters = new Parameters(['sort' => 'firstname,lastname']);

$parameters->getSort(['firstname']);
}

public function testGetOffsetParsesThePageOffset()
{
$parameters = new Parameters(['page' => ['offset' => 10]]);
Expand All @@ -64,12 +86,13 @@ public function testGetOffsetParsesThePageOffset()

/**
* @expectedException \Tobscure\JsonApi\Exception\InvalidParameterException
* @expectedExceptionCode 2
*/
public function testGetOffsetIsAtLeastZero()
{
$parameters = new Parameters(['page' => ['offset' => -5]]);

$this->assertEquals(0, $parameters->getOffset());
$parameters->getOffset();
}

public function testGetOffsetParsesThePageNumber()
Expand Down

0 comments on commit d9eccd9

Please sign in to comment.