Created
January 10, 2019 07:06
-
-
Save einnar82/92799f3efc462059efeeec881f4bf710 to your computer and use it in GitHub Desktop.
Override ModelNotFoundException in Laravel
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\Auth\AuthenticationException; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Psr7\Request; | |
use Illuminate\Database\Eloquent\ModelNotFoundException; | |
class Handler extends ExceptionHandler | |
{ | |
/** | |
* A list of the exception types that are not reported. | |
* | |
* @var array | |
*/ | |
protected $dontReport = [ | |
// | |
]; | |
/** | |
* A list of the inputs that are never flashed for validation exceptions. | |
* | |
* @var array | |
*/ | |
protected $dontFlash = [ | |
'password', | |
'password_confirmation', | |
]; | |
/** | |
* Report or log an exception. | |
* | |
* @param \Exception $exception | |
* @return void | |
* @throws Exception | |
*/ | |
public function report(Exception $exception) | |
{ | |
parent::report($exception); | |
} | |
/** | |
* Render an exception into an HTTP response. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Exception $exception | |
* @return \Illuminate\Http\Response | |
*/ | |
public function render($request, Exception $exception) | |
{ | |
if ($exception instanceof ModelNotFoundException) | |
{ | |
return response()->json(['message' => 'No '.lcfirst(substr($exception->getModel(), strrpos($exception->getModel(), '\\') + 1)).' found!'], 404); | |
} | |
return parent::render($request, $exception); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment