Skip to content

Commit

Permalink
feat: use new exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
albertcht committed Sep 7, 2024
1 parent b9bf4b3 commit 2e9cd7f
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 92 deletions.
38 changes: 38 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Exceptions;

use SwooleTW\Hyperf\Foundation\Exceptions\Handler as ExceptionHandler;
use SwooleTW\Hyperf\Http\Request;
use Throwable;

class Handler extends ExceptionHandler
{
/**
* The list of the inputs that are never flashed to the session on validation exceptions.
*
* @var array<int, string>
*/
protected array $dontFlash = [
'current_password',
'password',
'password_confirmation',
];

/**
* Register the exception handling callbacks for the application.
*/
public function register(): void
{
// return json when path start with `api`
$this->shouldRenderJsonWhen(function (Request $request, Throwable $e) {
return str_starts_with($path = $request->path(), 'api')
&& (strlen($path) === 3 || $path[3] === '/');
});

$this->reportable(function (Throwable $e) {
});
}
}
85 changes: 0 additions & 85 deletions app/Exceptions/Handlers/ApiExceptionHandler.php

This file was deleted.

4 changes: 4 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Kernel extends HttpKernel
'web' => [
// \SwooleTW\Hyperf\Router\Middleware\SubstituteBindings::class,
// \SwooleTW\Hyperf\Cookie\Middleware\AddQueuedCookiesToResponse::class,
// \Hyperf\Session\Middleware\SessionMiddleware::class,
// \Hyperf\ViewEngine\Http\Middleware\ShareErrorsFromSession::class,
],

'api' => [
Expand Down Expand Up @@ -59,5 +61,7 @@ class Kernel extends HttpKernel
protected array $middlewarePriority = [
// \SwooleTW\Hyperf\Router\Middleware\ThrottleRequests::class,
// \SwooleTW\Hyperf\Router\Middleware\SubstituteBindings::class,
// \Hyperf\Session\Middleware\SessionMiddleware::class,
// \Hyperf\ViewEngine\Http\Middleware\ShareErrorsFromSession::class,
];
}
1 change: 0 additions & 1 deletion app/Http/Middleware/ConvertEmptyStringsToNull.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ class ConvertEmptyStringsToNull extends Middleware
* @var array<int, string>
*/
protected array $except = [
//
];
}
1 change: 0 additions & 1 deletion app/Http/Middleware/TrimStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ class TrimStrings extends Middleware
* @var array<int, string>
*/
protected array $except = [
//
];
}
3 changes: 1 addition & 2 deletions config/exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
return [
'handler' => [
'http' => [
App\Exceptions\Handlers\ApiExceptionHandler::class,
Hyperf\ExceptionHandler\Handler\WhoopsExceptionHandler::class,
App\Exceptions\Handler::class,
],
],
];
5 changes: 2 additions & 3 deletions config/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
'engine' => HyperfViewEngine::class,
'mode' => Mode::SYNC,
'config' => [
'view_path' => base_path('resources/views/'),
'cache_path' => storage_path('framework/views/'),
'view_path' => base_path('resources/views'),
'cache_path' => storage_path('framework/views'),
],
'components' => [
//
],
];
2 changes: 2 additions & 0 deletions storage/framework/sessions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore

0 comments on commit 2e9cd7f

Please sign in to comment.