Skip to content

Commit

Permalink
Limit allowed codes for api exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Oct 2, 2018
1 parent bca3184 commit 2da0a7c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Exception/Api/ClientErrorException.php
Expand Up @@ -3,6 +3,7 @@
namespace Apitte\Core\Exception\Api;

use Apitte\Core\Exception\ApiException;
use Apitte\Core\Exception\Logical\InvalidArgumentException;
use Throwable;

/**
Expand All @@ -16,6 +17,10 @@ class ClientErrorException extends ApiException
*/
public function __construct(string $message = '', int $code = 400, ?Throwable $previous = null, $context = null)
{
if ($code < 400 || $code > 499) {
throw new InvalidArgumentException(sprintf('%s code could be only in range from 400 to 499', static::class));
}

parent::__construct($message, $code, $previous, $context);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Exception/Api/ServerErrorException.php
Expand Up @@ -3,6 +3,7 @@
namespace Apitte\Core\Exception\Api;

use Apitte\Core\Exception\ApiException;
use Apitte\Core\Exception\Logical\InvalidArgumentException;
use Throwable;

/**
Expand All @@ -13,6 +14,10 @@ class ServerErrorException extends ApiException

public function __construct(string $message = '', int $code = 500, ?Throwable $previous = null)
{
if ($code < 500 || $code > 599) {
throw new InvalidArgumentException(sprintf('%s code could be only in range from 500 to 599', static::class));
}

parent::__construct($message, $code, $previous);
}

Expand Down

0 comments on commit 2da0a7c

Please sign in to comment.