Files
portal-api/app/Exceptions/Handler.php
T
2026-02-11 11:18:35 +07:00

67 lines
1.8 KiB
PHP

<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array<int, class-string<Throwable>>
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array<int, string>
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->renderable(function (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e, $request) {
if (!$request->expectsJson() && !$request->is('v1/*') && !$request->is('api/*')) {
return null;
}
$previous = $e->getPrevious();
if ($previous instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
$modelClass = $previous->getModel();
$pageType = match ($modelClass) {
\App\Models\Product::class => 'service',
\App\Models\Customer::class => 'client',
\App\Models\Post::class => 'post',
default => 'unknown',
};
return response()->json([
'error' => true,
'message' => 'Dữ liệu không tồn tại',
'page_type' => $pageType,
], 404);
}
return response()->json([
'error' => true,
'message' => 'API Route not found',
], 404);
});
}
}