ldc-mesbr-app/app/Exceptions/Handler.php

68 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2019-01-15 14:17:15 +01:00
<?php
namespace App\Exceptions;
2021-04-20 19:51:30 +02:00
use App\Http\Resources\StatusResource;
use Illuminate\Database\Eloquent\ModelNotFoundException;
2025-04-02 15:14:41 +00:00
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
2021-04-20 12:22:04 +02:00
use Throwable;
2019-01-15 14:17:15 +01:00
class Handler extends ExceptionHandler
{
2025-04-02 15:14:41 +00:00
/**
* A list of exception types with their corresponding custom log levels.
*
* @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
*/
protected $levels = [];
2021-04-27 17:29:47 +02:00
2019-01-15 14:17:15 +01:00
/**
* A list of the exception types that are not reported.
*
2025-04-02 15:14:41 +00:00
* @var array<int, class-string<\Throwable>>
2019-01-15 14:17:15 +01:00
*/
2026-10-28 13:23:49 +01:00
2025-04-02 15:14:41 +00:00
protected $dontReport = [];
2019-01-15 14:17:15 +01:00
/**
2025-04-02 15:14:41 +00:00
* A list of the inputs that are never flashed to the session on validation exceptions.
2019-01-15 14:17:15 +01:00
*
2025-04-02 15:14:41 +00:00
* @var array<int, string>
2019-01-15 14:17:15 +01:00
*/
2026-10-28 13:23:49 +01:00
2019-01-15 14:17:15 +01:00
protected $dontFlash = [
2025-04-02 15:14:41 +00:00
'current_password',
2019-01-15 14:17:15 +01:00
'password',
'password_confirmation',
];
/**
2025-04-02 15:14:41 +00:00
* Register the exception handling callbacks for the application.
2019-01-15 14:17:15 +01:00
*
* @return void
*/
2025-04-02 15:14:41 +00:00
public function register()
2019-01-15 14:17:15 +01:00
{
2026-10-28 12:27:46 +01:00
$this->reportable(function (Throwable $e): void {
2025-04-02 15:14:41 +00:00
if (app()->bound('sentry')) {
2026-10-28 12:27:46 +01:00
resolve('sentry')->captureException($e);
2025-04-02 15:14:41 +00:00
}
});
2019-01-15 14:17:15 +01:00
}
/**
* Render an exception into an HTTP response.
*
2025-04-03 09:30:16 +02:00
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
2021-04-20 19:51:30 +02:00
* @return \Symfony\Component\HttpFoundation\Response
2019-01-15 14:17:15 +01:00
*/
2021-04-20 12:22:04 +02:00
public function render($request, Throwable $exception)
2019-01-15 14:17:15 +01:00
{
2021-04-27 17:29:47 +02:00
if ($request->wantsJson() && $exception instanceof ModelNotFoundException) {
2021-04-20 19:51:30 +02:00
return response()->json(new StatusResource(['status' => 'KO', 'code' => 404, 'message' => 'Resource not found.']), 404);
}
2019-01-15 14:17:15 +01:00
return parent::render($request, $exception);
}
}