Skip to content

Commit

Permalink
laravel 10 support and strict types
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Minenok committed Apr 12, 2023
1 parent c431ee1 commit 1816514
Show file tree
Hide file tree
Showing 26 changed files with 162 additions and 305 deletions.
82 changes: 44 additions & 38 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
{
"name": "lanin/laravel-api-exceptions",
"description": "All in one solution for exception for JSON REST APIs on Laravel and Lumen.",
"keywords": ["laravel", "framework", "api", "exceptions", "json"],
"license": "MIT",
"authors": [
{
"name": "Maxim Lanin",
"email": "[email protected]",
"homepage": "https://blog.lanin.me"
}
],
"require": {
"php": "^7.2|^8.0",
"illuminate/support": "^7.0|^8.0|^9.0"
},
"require-dev": {
"orchestra/testbench": "^5.0",
"phpunit/phpunit": "^8.0",
"mockery/mockery": "^1.0"
},
"autoload": {
"psr-4": {
"Lanin\\Laravel\\ApiExceptions\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Lanin\\Laravel\\ApiExceptions\\Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"laravel": {
"providers": [
"Lanin\\Laravel\\ApiExceptions\\ApiExceptionsServiceProvider"
]
}
"name": "lanin/laravel-api-exceptions",
"description": "All in one solution for exception for JSON REST APIs on Laravel and Lumen.",
"keywords": [
"laravel",
"framework",
"api",
"exceptions",
"json"
],
"license": "MIT",
"authors": [
{
"name": "Maxim Lanin",
"email": "[email protected]",
"homepage": "https://blog.lanin.me"
}
],
"require": {
"php": ">=8.1",
"illuminate/support": "^10.0"
},
"require-dev": {
"orchestra/testbench": "^8.3",
"phpunit/phpunit": ">=8.0",
"mockery/mockery": "^1.0"
},
"autoload": {
"psr-4": {
"Lanin\\Laravel\\ApiExceptions\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Lanin\\Laravel\\ApiExceptions\\Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"laravel": {
"providers": [
"Lanin\\Laravel\\ApiExceptions\\ApiExceptionsServiceProvider"
]
}
}
}
26 changes: 10 additions & 16 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php"
colors="true" processIsolation="false" stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache"
backupStaticProperties="false">
<coverage>
<include>
<directory suffix=".php">./src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</phpunit>
</phpunit>
67 changes: 19 additions & 48 deletions src/ApiException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Lanin\Laravel\ApiExceptions;

use Illuminate\Contracts\Support\Arrayable;
Expand All @@ -10,59 +12,32 @@

abstract class ApiException extends IdException implements Jsonable, \JsonSerializable, Arrayable
{
protected $headers = [];

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

public function __construct(
int $statusCode = 0,
string $id = '',
string $message = '',
?\Throwable $previous = null,
protected array $headers = [],
) {
parent::__construct($id, $message, $previous, $statusCode);
}

/**
* Return headers array.
*
* @return array
*/
public function getHeaders()
public function getHeaders(): array
{
return $this->headers;
}

/**
* Convert the object into something JSON serializable.
*
* @return array
*/
public function jsonSerialize()
public function jsonSerialize(): mixed
{
return $this->toArray();
}

/**
* Convert exception to JSON.
*
* @param int $options
* @return array
*/
public function toJson($options = 0)
public function toJson($options = 0): string
{
return json_encode($this->toArray());
}

/**
* Convert exception to array.
*
* @return array
*/
public function toArray()
public function toArray(): array
{
$e = $this;

Expand All @@ -76,7 +51,7 @@ public function toArray()

if ($e instanceof ApiException) {
$meta = $this->getMeta();
if (! empty($meta)) {
if (!empty($meta)) {
$return['meta'] = $meta;
}
}
Expand All @@ -88,20 +63,16 @@ public function toArray()
return $return;
}

/**
* Prepare exception for report.
*
* @return string
*/
public function toReport()
public function toReport(): self
{
return $this;
}

/**
* Add extra info to the output.
*
* @return mixed
*/
public function getMeta() {}
public function getMeta(): array
{
return [];
}
}
2 changes: 2 additions & 0 deletions src/ApiExceptionsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Lanin\Laravel\ApiExceptions;

use Illuminate\Support\ServiceProvider;
Expand Down
8 changes: 3 additions & 5 deletions src/BadRequestApiException.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
<?php

declare(strict_types=1);

namespace Lanin\Laravel\ApiExceptions;

use Lanin\Laravel\ApiExceptions\Contracts\DontReport;

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

declare(strict_types=1);

namespace Lanin\Laravel\ApiExceptions;

use Lanin\Laravel\ApiExceptions\Contracts\DontReport;

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

declare(strict_types=1);

namespace Lanin\Laravel\ApiExceptions\Contracts;

interface DontReport
{

}
}
5 changes: 3 additions & 2 deletions src/Contracts/ShowsPrevious.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

declare(strict_types=1);

namespace Lanin\Laravel\ApiExceptions\Contracts;

interface ShowsPrevious
{

}
}
5 changes: 3 additions & 2 deletions src/Contracts/ShowsTrace.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

declare(strict_types=1);

namespace Lanin\Laravel\ApiExceptions\Contracts;

interface ShowsTrace
{

}
}
25 changes: 9 additions & 16 deletions src/ExceptionHandlerTrait.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php

declare(strict_types=1);

namespace Lanin\Laravel\ApiExceptions;

use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand All @@ -14,22 +18,17 @@ trait ExceptionHandlerTrait
/**
* Report or log an exception.
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Throwable $e
* @return void
* @throws \Throwable
*/
public function report(\Throwable $e)
public function report(\Throwable $e): void
{
parent::report($e instanceof ApiException ? $e->toReport() : $e);
}

/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $e
* @return \Illuminate\Http\Response
* @param Request $request
* @return JsonResponse
*/
public function render($request, \Throwable $e)
{
Expand All @@ -42,11 +41,8 @@ public function render($request, \Throwable $e)

/**
* Define exception.
*
* @param \Throwable $e
* @return ApiException
*/
protected function resolveException(\Throwable $e)
protected function resolveException(\Throwable $e): ApiException
{
switch (true) {
case $e instanceof ApiException:
Expand Down Expand Up @@ -77,11 +73,8 @@ protected function resolveException(\Throwable $e)

/**
* Format error message for API response.
*
* @param ApiException $exception
* @return mixed
*/
protected function formatApiResponse(ApiException $exception)
protected function formatApiResponse(ApiException $exception): array
{
return $exception->toArray();
}
Expand Down
Loading

0 comments on commit 1816514

Please sign in to comment.