Created
February 25, 2015 13:07
-
-
Save emanuelpessoaa/241579f3e2b57703a4cc to your computer and use it in GitHub Desktop.
Laravel 5 HttpException Error 404
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Exceptions; | |
use Exception; | |
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; | |
use Illuminate\Database\Eloquent\ModelNotFoundException; | |
class Handler extends ExceptionHandler { | |
/** | |
* A list of the exception types that should not be reported. | |
* | |
* @var array | |
*/ | |
protected $dontReport = [ | |
'Symfony\Component\HttpKernel\Exception\HttpException' | |
]; | |
/** | |
* Throw a 404 is a model record isn't found | |
* @param ModelNotFoundException | |
* @return [type] | |
*/ | |
protected function renderModelNotFoundException(ModelNotFoundException $e) | |
{ | |
if (view()->exists('errors.404')) | |
{ | |
return response()->view('errors.404', [], 404); | |
} | |
else | |
{ | |
return (new SymfonyDisplayer(config('app.debug'))) | |
->createResponse($e); | |
} | |
} | |
/** | |
* Report or log an exception. | |
* | |
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. | |
* | |
* @param \Exception $e | |
* @return void | |
*/ | |
public function report(Exception $e) | |
{ | |
return parent::report($e); | |
} | |
/** | |
* Render an exception into an HTTP response. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Exception $e | |
* @return \Illuminate\Http\Response | |
*/ | |
public function render($request, Exception $e){ | |
// if($e->getStatusCode()===404){ | |
// return response()->view('errors.404', [], 404); | |
// } | |
// return parent::render($request, $e); | |
if ($this->isHttpException($e)){ | |
return $this->renderHttpException($e); | |
}elseif ($e instanceof ModelNotFoundException){ | |
return $this->renderModelNotFoundException($e); | |
}else{ | |
return parent::render($request, $e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment