Skip to content

Commit 6358e85

Browse files
committed
Clean up old excception handling in httpkernel
1 parent 199ce6d commit 6358e85

File tree

5 files changed

+50
-151
lines changed

5 files changed

+50
-151
lines changed

app/Core/Events.php

+22-7
Original file line numberDiff line numberDiff line change
@@ -520,15 +520,20 @@ public function listen($events, $listener = null) {}
520520
* @param string $eventName
521521
* @return bool
522522
*/
523-
public function hasListeners($eventName) {}
523+
public function hasListeners($eventName) {
524+
throw new \Exception("Not implemented");
525+
return false;
526+
}
524527

525528
/**
526529
* Register an event subscriber with the dispatcher.
527530
*
528531
* @param object|string $subscriber
529532
* @return void
530533
*/
531-
public function subscribe($subscriber) {}
534+
public function subscribe($subscriber) {
535+
throw new \Exception("Not implemented");
536+
}
532537

533538
/**
534539
* Dispatch an event until the first non-null response is returned.
@@ -537,7 +542,9 @@ public function subscribe($subscriber) {}
537542
* @param mixed $payload
538543
* @return mixed
539544
*/
540-
public function until($event, $payload = []) {}
545+
public function until($event, $payload = []) {
546+
throw new \Exception("Not implemented");
547+
}
541548

542549

543550
/**
@@ -547,28 +554,36 @@ public function until($event, $payload = []) {}
547554
* @param array $payload
548555
* @return void
549556
*/
550-
public function push($event, $payload = []) {}
557+
public function push($event, $payload = []) {
558+
throw new \Exception("Not implemented");
559+
}
551560

552561
/**
553562
* Flush a set of pushed events.
554563
*
555564
* @param string $event
556565
* @return void
557566
*/
558-
public function flush($event) {}
567+
public function flush($event) {
568+
throw new \Exception("Not implemented");
569+
}
559570

560571
/**
561572
* Remove a set of listeners from the dispatcher.
562573
*
563574
* @param string $event
564575
* @return void
565576
*/
566-
public function forget($event) {}
577+
public function forget($event) {
578+
throw new \Exception("Not implemented");
579+
}
567580

568581
/**
569582
* Forget all of the queued listeners.
570583
*
571584
* @return void
572585
*/
573-
public function forgetPushed() {}
586+
public function forgetPushed() {
587+
throw new \Exception("Not implemented");
588+
}
574589
}

app/Core/ExceptionHandler.php

+11-100
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Illuminate\Session\TokenMismatchException;
1616
use Illuminate\Support\Arr;
1717
use Illuminate\Support\Facades\Auth;
18+
use Illuminate\Support\Facades\Log;
1819
use Illuminate\Support\Reflector;
1920
use Illuminate\Support\Traits\ReflectsClosures;
2021
use Illuminate\Support\ViewErrorBag;
@@ -81,16 +82,10 @@ class ExceptionHandler implements ExceptionHandlerContract
8182
* @var string[]
8283
*/
8384
protected $internalDontReport = [
84-
AuthenticationException::class,
85-
AuthorizationException::class,
8685
HttpException::class,
8786
HttpResponseException::class,
88-
ModelNotFoundException::class,
89-
MultipleRecordsFoundException::class,
90-
RecordsNotFoundException::class,
9187
SuspiciousOperationException::class,
9288
TokenMismatchException::class,
93-
ValidationException::class,
9489
];
9590

9691
/**
@@ -234,20 +229,16 @@ public function report(Throwable $e)
234229
}
235230
}
236231

237-
try {
238-
$logger = $this->container->make(LoggerInterface::class);
239-
} catch (Exception $ex) {
240-
throw $e;
241-
}
242232

243-
$logger->error(
233+
Log::error(
244234
$e->getMessage(),
245235
array_merge(
246236
$this->exceptionContext($e),
247237
$this->context(),
248238
['exception' => $e]
249239
)
250240
);
241+
251242
}
252243

253244
/**
@@ -300,8 +291,7 @@ protected function context()
300291
{
301292
try {
302293
return array_filter([
303-
'userId' => Auth::id(),
304-
// 'email' => optional(Auth::user())->email,
294+
305295
]);
306296
} catch (Throwable $e) {
307297
return [];
@@ -341,10 +331,6 @@ public function render($request, Throwable $e)
341331

342332
if ($e instanceof HttpResponseException) {
343333
return $e->getResponse();
344-
} elseif ($e instanceof AuthenticationException) {
345-
return $this->unauthenticated($request, $e);
346-
} elseif ($e instanceof ValidationException) {
347-
return $this->convertValidationExceptionToResponse($e, $request);
348334
}
349335

350336
return $this->shouldReturnJson($request, $e)
@@ -377,82 +363,10 @@ protected function mapException(Throwable $e)
377363
*/
378364
protected function prepareException(Throwable $e)
379365
{
380-
if ($e instanceof ModelNotFoundException) {
381-
$e = new NotFoundHttpException($e->getMessage(), $e);
382-
} elseif ($e instanceof AuthorizationException) {
383-
$e = new AccessDeniedHttpException($e->getMessage(), $e);
384-
} elseif ($e instanceof TokenMismatchException) {
385-
$e = new HttpException(419, $e->getMessage(), $e);
386-
} elseif ($e instanceof SuspiciousOperationException) {
387-
$e = new NotFoundHttpException('Bad hostname provided.', $e);
388-
} elseif ($e instanceof RecordsNotFoundException) {
389-
$e = new NotFoundHttpException('Not found.', $e);
390-
}
391366

392367
return $e;
393368
}
394369

395-
/**
396-
* Convert an authentication exception into a response.
397-
*
398-
* @param \Illuminate\Http\Request $request
399-
* @param \Illuminate\Auth\AuthenticationException $exception
400-
* @return \Symfony\Component\HttpFoundation\Response
401-
*/
402-
protected function unauthenticated($request, AuthenticationException $exception)
403-
{
404-
return $this->shouldReturnJson($request, $exception)
405-
? response()->json(['message' => $exception->getMessage()], 401)
406-
: redirect()->guest($exception->redirectTo() ?? route('login'));
407-
}
408-
409-
/**
410-
* Create a response object from the given validation exception.
411-
*
412-
* @param \Illuminate\Validation\ValidationException $e
413-
* @param \Illuminate\Http\Request $request
414-
* @return \Symfony\Component\HttpFoundation\Response
415-
*/
416-
protected function convertValidationExceptionToResponse(ValidationException $e, $request)
417-
{
418-
if ($e->response) {
419-
return $e->response;
420-
}
421-
422-
return $this->shouldReturnJson($request, $e)
423-
? $this->invalidJson($request, $e)
424-
: $this->invalid($request, $e);
425-
}
426-
427-
/**
428-
* Convert a validation exception into a response.
429-
*
430-
* @param \Illuminate\Http\Request $request
431-
* @param \Illuminate\Validation\ValidationException $exception
432-
* @return \Illuminate\Http\Response
433-
*/
434-
protected function invalid($request, ValidationException $exception)
435-
{
436-
return redirect($exception->redirectTo ?? url()->previous())
437-
->withInput(Arr::except($request->input(), $this->dontFlash))
438-
->withErrors($exception->errors(), $request->input('_error_bag', $exception->errorBag));
439-
}
440-
441-
/**
442-
* Convert a validation exception into a JSON response.
443-
*
444-
* @param \Illuminate\Http\Request $request
445-
* @param \Illuminate\Validation\ValidationException $exception
446-
* @return \Illuminate\Http\JsonResponse
447-
*/
448-
protected function invalidJson($request, ValidationException $exception)
449-
{
450-
return response()->json([
451-
'message' => $exception->getMessage(),
452-
'errors' => $exception->errors(),
453-
], $exception->status);
454-
}
455-
456370
/**
457371
* Determine if the exception handler response should be JSON.
458372
*
@@ -573,16 +487,13 @@ protected function renderExceptionWithSymfony(Throwable $e, $debug)
573487
*/
574488
protected function renderHttpException(HttpExceptionInterface $e)
575489
{
576-
//$this->registerErrorViewPaths();
577490

578-
if (view()->exists($view = $this->getHttpExceptionView($e))) {
579-
return response()->view($view, [
580-
'errors' => new ViewErrorBag(),
581-
'exception' => $e,
582-
], $e->getStatusCode(), $e->getHeaders());
491+
try {
492+
$view = $this->getHttpExceptionView($e);
493+
return app()->make(Template::class)->display($view, "error", $e->getStatusCode());
494+
}catch(Throwable $e){
495+
return $this->convertExceptionToResponse($e);
583496
}
584-
585-
return $this->convertExceptionToResponse($e);
586497
}
587498

588499
/**
@@ -592,7 +503,7 @@ protected function renderHttpException(HttpExceptionInterface $e)
592503
*/
593504
protected function registerErrorViewPaths()
594505
{
595-
(new RegisterErrorViewPaths())();
506+
596507
}
597508

598509
/**
@@ -603,7 +514,7 @@ protected function registerErrorViewPaths()
603514
*/
604515
protected function getHttpExceptionView(HttpExceptionInterface $e)
605516
{
606-
return "error/error{$e->getStatusCode()}";
517+
return "errors.error{$e->getStatusCode()}";
607518
}
608519

609520
/**

app/Core/HttpKernel.php

+15-40
Original file line numberDiff line numberDiff line change
@@ -54,49 +54,24 @@ public function handle($request)
5454
{
5555
$this->requestStartedAt = microtime(true);
5656

57-
try {
58-
59-
//Main Pipeline
60-
$response = (new Pipeline($this->app))
57+
//Main Pipeline
58+
$response = (new Pipeline($this->app))
59+
->send($request)
60+
->through($this->getMiddleware())
61+
->then(fn ($request) =>
62+
//Then run through plugin pipeline
63+
(new Pipeline($this->app))
6164
->send($request)
62-
->through($this->getMiddleware())
63-
->then(fn ($request) =>
64-
//Then run through plugin pipeline
65-
(new Pipeline($this->app))
66-
->send($request)
67-
->through(self::dispatch_filter(
68-
hook: 'plugins_middleware',
69-
payload: [],
70-
function: 'handle',
71-
))
72-
->then(fn () => Frontcontroller::dispatch_request($request))
73-
);
74-
75-
return self::dispatch_filter('beforeSendResponse', $response);
76-
77-
} catch (HttpResponseException $e) {
78-
report($e);
79-
return $e->getResponse();
80-
} catch (\Throwable $e) {
81-
82-
report($e);
83-
84-
if (! $this->app->make(Environment::class)->debug) {
85-
86-
return $this->app->make(Template::class)->display('errors.error500', 'error');
87-
}
65+
->through(self::dispatch_filter(
66+
hook: 'plugins_middleware',
67+
payload: [],
68+
function: 'handle',
69+
))
70+
->then(fn () => Frontcontroller::dispatch_request($request))
71+
);
8872

89-
if ($request instanceof HtmxRequest) {
90-
/** @todo Replace with a proper error template for htmx requests */
91-
return new Response(sprintf(
92-
'<dialog style="%s" open>%s</dialog>',
93-
'width: 90vw; height: 90vh; z-index: 9999999; position: fixed; top: 5vh; left: 5vh; overflow: scroll',
94-
(new HtmlErrorRenderer(true))->render($e)->getAsString(),
95-
));
96-
}
73+
return self::dispatch_filter('beforeSendResponse', $response);
9774

98-
throw $e;
99-
}
10075
}
10176

10277
/**

app/Core/IncomingRequest.php

+2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ public function expectsJson()
176176
if($this instanceof CliRequest || $this->isApiOrCronRequest()) {
177177
return true;
178178
}
179+
180+
return false;
179181
}
180182

181183
}

blocklist.json

-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
"tw-absolute",
33
"tw-relative",
44
"tw-sticky",
5-
"tw-left-0",
65
"tw-right-[10px]",
76
"tw-top-0",
87
"tw-top-[10px]",
9-
"tw-top-[50px]",
108
"tw-z-10",
119
"tw-z-20",
1210
"tw-float-right",
@@ -17,7 +15,6 @@
1715
"tw-mb-l",
1816
"tw-mb-m",
1917
"tw-ml-0",
20-
"tw-ml-[100px]",
2118
"tw-ml-[10px]",
2219
"tw-ml-base",
2320
"tw-ml-m",
@@ -69,7 +66,6 @@
6966
"tw-bg-primary",
7067
"tw-bg-red-500",
7168
"tw-bg-yellow-500",
72-
"tw-p-0",
7369
"tw-p-[10px]",
7470
"tw-p-m",
7571
"tw-p-none",

0 commit comments

Comments
 (0)