2022-08-12 07:56:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Exceptions;
|
|
|
|
|
2022-11-07 01:13:40 +00:00
|
|
|
use Illuminate\Auth\AuthenticationException;
|
2022-11-08 13:01:43 +00:00
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
2022-12-11 11:47:30 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Illuminate\Http\Request;
|
2022-11-08 13:01:43 +00:00
|
|
|
use Throwable;
|
2022-08-12 07:56:56 +00:00
|
|
|
|
|
|
|
class Handler extends ExceptionHandler
|
|
|
|
{
|
|
|
|
/**
|
2022-11-06 11:28:22 +00:00
|
|
|
* A list of exception types with their corresponding custom log levels.
|
2022-08-12 07:56:56 +00:00
|
|
|
*
|
2022-12-27 16:25:22 +00:00
|
|
|
* @var array<class-string<Throwable>, \Psr\Log\LogLevel::*>
|
2022-08-12 07:56:56 +00:00
|
|
|
*/
|
2022-11-06 11:28:22 +00:00
|
|
|
protected $levels = [
|
|
|
|
//
|
2022-08-12 07:56:56 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2022-11-06 11:28:22 +00:00
|
|
|
* A list of the exception types that are not reported.
|
2022-09-08 16:12:02 +00:00
|
|
|
*
|
2022-12-27 16:25:22 +00:00
|
|
|
* @var array<int, class-string<Throwable>>
|
2022-08-12 07:56:56 +00:00
|
|
|
*/
|
2022-11-06 11:28:22 +00:00
|
|
|
protected $dontReport = [
|
|
|
|
//
|
|
|
|
];
|
2022-08-12 07:56:56 +00:00
|
|
|
|
|
|
|
/**
|
2022-11-06 11:28:22 +00:00
|
|
|
* A list of the inputs that are never flashed to the session on validation exceptions.
|
2022-08-12 07:56:56 +00:00
|
|
|
*
|
2022-11-06 11:28:22 +00:00
|
|
|
* @var array<int, string>
|
|
|
|
*/
|
|
|
|
protected $dontFlash = [
|
|
|
|
'current_password',
|
|
|
|
'password',
|
|
|
|
'password_confirmation',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the exception handling callbacks for the application.
|
2022-08-12 07:56:56 +00:00
|
|
|
*/
|
2022-12-11 11:47:30 +00:00
|
|
|
public function register(): void
|
2022-08-12 07:56:56 +00:00
|
|
|
{
|
2023-01-10 13:42:27 +00:00
|
|
|
// $this->reportable(function (Throwable $e) {
|
|
|
|
// // custom json 404 response
|
|
|
|
// if ($e instanceof NotFoundHttpException) {
|
|
|
|
// return response()->json([
|
|
|
|
// 'message' => 'Not Found',
|
|
|
|
// 'errors' => [
|
|
|
|
// 'code' => 404,
|
|
|
|
// 'message' => 'Not Found'
|
|
|
|
// ]
|
|
|
|
// ], 404);
|
|
|
|
// }
|
|
|
|
// return response();
|
|
|
|
// });
|
2023-01-14 21:37:25 +00:00
|
|
|
|
|
|
|
$this->reportable(function (Throwable $e) {
|
|
|
|
//
|
|
|
|
});
|
2022-08-12 07:56:56 +00:00
|
|
|
}
|
2022-11-08 13:01:43 +00:00
|
|
|
|
2022-11-07 01:13:40 +00:00
|
|
|
/**
|
|
|
|
* Convert an authentication exception into an unauthenticated response.
|
|
|
|
*
|
2023-02-07 09:04:11 +00:00
|
|
|
* @param Request $request
|
|
|
|
* @param AuthenticationException $exception
|
2022-12-11 11:47:30 +00:00
|
|
|
* @return JsonResponse|RedirectResponse
|
2022-11-07 01:13:40 +00:00
|
|
|
*/
|
2022-12-11 11:47:30 +00:00
|
|
|
protected function unauthenticated($request, AuthenticationException $exception): JsonResponse|RedirectResponse
|
2022-11-07 01:13:40 +00:00
|
|
|
{
|
2022-11-14 10:57:31 +00:00
|
|
|
// if json request
|
2022-11-07 01:13:40 +00:00
|
|
|
if ($request->expectsJson()) {
|
2022-11-14 10:57:31 +00:00
|
|
|
return response()->json(['message' => $exception->getMessage()], 401);
|
2022-11-07 01:13:40 +00:00
|
|
|
}
|
2022-11-08 13:01:43 +00:00
|
|
|
|
2022-11-07 01:13:40 +00:00
|
|
|
return redirect()->guest(route('index'));
|
|
|
|
}
|
2022-08-12 07:56:56 +00:00
|
|
|
}
|