81 lines
2.3 KiB
PHP
81 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Exceptions;
|
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Database\UniqueConstraintViolationException;
|
|
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()
|
|
{
|
|
// Duplicate constraint
|
|
$this->renderable(function (QueryException $e, $request) {
|
|
|
|
if ($e->errorInfo[1] == 1062) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => 'Dữ liệu đã tồn tại'
|
|
], 422);
|
|
}
|
|
});
|
|
|
|
|
|
$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);
|
|
});
|
|
}
|
|
}
|