Skip to content

Commit

Permalink
Merge pull request #16 from mlanin/bugfix/throwable
Browse files Browse the repository at this point in the history
Use \Throwable instead of default \Exception
  • Loading branch information
mlanin authored Feb 17, 2021
2 parents 5fbd782 + c61f60c commit 86aa2a8
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 43 deletions.
12 changes: 6 additions & 6 deletions src/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ abstract class ApiException extends IdException implements Jsonable, \JsonSerial
protected $headers = [];

/**
* @param int $statusCode
* @param string $id
* @param string $message
* @param \Exception $previous
* @param array $headers
* @param int $statusCode
* @param string $id
* @param string $message
* @param \Throwable|null $previous
* @param array $headers
*/
public function __construct($statusCode = 0, $id = '', $message = '', \Exception $previous = null, array $headers = [])
public function __construct($statusCode = 0, $id = '', $message = '', ?\Throwable $previous = null, array $headers = [])
{
$this->headers = $headers;

Expand Down
5 changes: 2 additions & 3 deletions src/BadRequestApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Lanin\Laravel\ApiExceptions;

use Exception;
use Lanin\Laravel\ApiExceptions\Contracts\DontReport;

class BadRequestApiException extends ApiException implements DontReport
{
/**
* @param string $message
* @param Exception $previous
* @param \Throwable|null $previous
*/
public function __construct($message = '', Exception $previous = null)
public function __construct($message = '', ?\Throwable $previous = null)
{
if (empty($message)) {
$message = 'The server cannot process the request due to its malformed syntax.';
Expand Down
5 changes: 2 additions & 3 deletions src/ConflictApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Lanin\Laravel\ApiExceptions;

use Exception;
use Lanin\Laravel\ApiExceptions\Contracts\DontReport;

class ConflictApiException extends ApiException implements DontReport
{
/**
* @param string $message
* @param Exception $previous
* @param \Throwable $previous
*/
public function __construct($message = '', Exception $previous = null)
public function __construct($message = '', ?\Throwable $previous = null)
{
if (empty($message)) {
$message = 'Request could not be processed because of conflict.';
Expand Down
15 changes: 7 additions & 8 deletions src/ExceptionHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Lanin\Laravel\ApiExceptions;

use Throwable;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
Expand All @@ -16,11 +15,11 @@ trait ExceptionHandlerTrait
* Report or log an exception.
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param Throwable $e
* @param \Throwable $e
* @return void
* @throws Exception
* @throws \Throwable
*/
public function report(Throwable $e)
public function report(\Throwable $e)
{
parent::report($e instanceof ApiException ? $e->toReport() : $e);
}
Expand All @@ -29,10 +28,10 @@ public function report(Throwable $e)
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param Throwable $e
* @param \Throwable $e
* @return \Illuminate\Http\Response
*/
public function render($request, Throwable $e)
public function render($request, \Throwable $e)
{
$e = $this->resolveException($e);

Expand All @@ -44,10 +43,10 @@ public function render($request, Throwable $e)
/**
* Define exception.
*
* @param Throwable $e
* @param \Throwable $e
* @return ApiException
*/
protected function resolveException(Throwable $e)
protected function resolveException(\Throwable $e)
{
switch (true) {
case $e instanceof ApiException:
Expand Down
5 changes: 2 additions & 3 deletions src/ForbiddenApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Lanin\Laravel\ApiExceptions;

use Exception;
use Lanin\Laravel\ApiExceptions\Contracts\DontReport;

class ForbiddenApiException extends ApiException implements DontReport
{
/**
* @param string $message
* @param Exception $previous
* @param \Throwable|null $previous
*/
public function __construct($message = '', Exception $previous = null)
public function __construct($message = '', ?\Throwable $previous = null)
{
if (empty($message)) {
$message = "You don't have permissions to perform this request.";
Expand Down
4 changes: 2 additions & 2 deletions src/IdException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class IdException extends \RuntimeException
/**
* @param string $id
* @param string $message
* @param \Exception $previous
* @param \Throwable|null $previous
* @param int $code
*/
public function __construct($id = '', $message = '', \Exception $previous = null, $code = 0)
public function __construct($id = '', $message = '', ?\Throwable $previous = null, $code = 0)
{
$this->id = $id;

Expand Down
5 changes: 2 additions & 3 deletions src/InternalServerErrorApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

namespace Lanin\Laravel\ApiExceptions;

use Exception;
use Lanin\Laravel\ApiExceptions\Contracts\ShowsPrevious;
use Lanin\Laravel\ApiExceptions\Contracts\ShowsTrace;

class InternalServerErrorApiException extends ApiException implements ShowsTrace, ShowsPrevious
{
/**
* @param string $message
* @param Exception $previous
* @param \Throwable|null $previous
*/
public function __construct($message = '', Exception $previous = null)
public function __construct($message = '', ?\Throwable $previous = null)
{
if (empty($message)) {
$message = 'The server encountered an internal error or misconfiguration and was unable to complete your request.';
Expand Down
5 changes: 2 additions & 3 deletions src/MethodNotAllowedApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Lanin\Laravel\ApiExceptions;

use Exception;
use Lanin\Laravel\ApiExceptions\Contracts\DontReport;

class MethodNotAllowedApiException extends ApiException implements DontReport
{
/**
* @param string $message
* @param Exception $previous
* @param \Throwable|null $previous
*/
public function __construct($message = '', Exception $previous = null)
public function __construct($message = '', ?\Throwable $previous = null)
{
if (empty($message)) {
$message = 'A request was made of a resource using a request method not supported by that resource.';
Expand Down
5 changes: 2 additions & 3 deletions src/NotFoundApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Lanin\Laravel\ApiExceptions;

use Exception;
use Lanin\Laravel\ApiExceptions\Contracts\DontReport;

class NotFoundApiException extends ApiException implements DontReport
{
/**
* @param string $message
* @param Exception $previous
* @param \Throwable|null $previous
*/
public function __construct($message = '', Exception $previous = null)
public function __construct($message = '', ?\Throwable $previous = null)
{
if (empty($message)) {
$message = 'Requested object not found.';
Expand Down
5 changes: 2 additions & 3 deletions src/TooManyRequestsApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Lanin\Laravel\ApiExceptions;

use Exception;
use Lanin\Laravel\ApiExceptions\Contracts\DontReport;

class TooManyRequestsApiException extends ApiException implements DontReport
Expand All @@ -16,9 +15,9 @@ class TooManyRequestsApiException extends ApiException implements DontReport
* @param int|null $retryAfter
* @param array $headers
* @param string $message
* @param Exception $previous
* @param \Throwable|null $previous
*/
public function __construct($retryAfter = null, $headers = [], $message = '', Exception $previous = null)
public function __construct($retryAfter = null, $headers = [], $message = '', ?\Throwable $previous = null)
{
$this->retryAfter = $retryAfter;

Expand Down
5 changes: 2 additions & 3 deletions src/UnauthorizedApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Lanin\Laravel\ApiExceptions;

use Exception;
use Lanin\Laravel\ApiExceptions\Contracts\DontReport;

class UnauthorizedApiException extends ApiException implements DontReport
{
/**
* @param string $message
* @param Exception $previous
* @param \Throwable|null $previous
*/
public function __construct($message = '', Exception $previous = null)
public function __construct($message = '', ?\Throwable $previous = null)
{
if (empty($message)) {
$message = 'Sent credentials are invalid.';
Expand Down
5 changes: 2 additions & 3 deletions src/ValidationFailedApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Lanin\Laravel\ApiExceptions;

use Exception;
use Lanin\Laravel\ApiExceptions\Contracts\DontReport;

class ValidationFailedApiException extends ApiException implements DontReport
Expand All @@ -17,9 +16,9 @@ class ValidationFailedApiException extends ApiException implements DontReport
*
* @param array $errors
* @param string $message
* @param Exception $previous
* @param \Throwable|null $previous
*/
public function __construct(array $errors, $message = '', Exception $previous = null)
public function __construct(array $errors, $message = '', ?\Throwable $previous = null)
{
$this->errors = $errors;

Expand Down

0 comments on commit 86aa2a8

Please sign in to comment.