Skip to content

Commit

Permalink
Convert Laravel's ModelNotFoundException into NotFoundHttpException, …
Browse files Browse the repository at this point in the history
…and display the stack trace
  • Loading branch information
Max Snow committed Sep 27, 2018
1 parent 1422535 commit c7942f0
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Exception/Handler.php
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\HttpFoundation\Response as BaseResponse;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Illuminate\Contracts\Debug\ExceptionHandler as IlluminateExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class Handler implements ExceptionHandler, IlluminateExceptionHandler
{
Expand Down Expand Up @@ -128,6 +129,11 @@ public function register(callable $callback)
*/
public function handle(Exception $exception)
{
// Convert Eloquent's 500 ModelNotFoundException into a 404 NotFoundHttpException
if ($exception instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
$exception = new NotFoundHttpException($exception->getMessage(), $exception);
}

foreach ($this->handlers as $hint => $handler) {
if (! $exception instanceof $hint) {
continue;
Expand Down Expand Up @@ -239,6 +245,16 @@ protected function prepareReplacements(Exception $exception)
'class' => get_class($exception),
'trace' => explode("\n", $exception->getTraceAsString()),
];

// Attach trace of previous exception, if exists
if (!is_null($exception->getPrevious())) {
$currentTrace = $replacements[':debug']['trace'];

$replacements[':debug']['trace'] = [
'previous' => explode("\n", $exception->getPrevious()->getTraceAsString()),
'current' => $currentTrace,
];
}
}

return array_merge($replacements, $this->replacements);
Expand Down

0 comments on commit c7942f0

Please sign in to comment.