Created
August 10, 2015 14:43
-
-
Save kirkbushell/570f621974acb631c8ad to your computer and use it in GitHub Desktop.
Custom exception handler
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 AwardForce\Library\Exceptions; | |
use AwardForce\Library\Http\FormInputOutdatedException; | |
use AwardForce\Library\Http\Messaging; | |
use AwardForce\Library\Traits\Respondable; | |
use AwardForce\Modules\Accounts\AccountNotFoundException; | |
use Exception; | |
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; | |
use Illuminate\Http\Response; | |
use Illuminate\Session\TokenMismatchException; | |
use Illuminate\Support\Facades\Lang; | |
use Symfony\Component\HttpKernel\Exception\HttpException; | |
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
class Handler extends ExceptionHandler | |
{ | |
use Messaging; | |
use Respondable; | |
protected $layout; | |
/** | |
* A list of the exception types that should not be reported. | |
* | |
* @var array | |
*/ | |
protected $dontReport = [ | |
AccountNotFoundException::class, | |
FormInputOutdatedException::class, | |
HttpException::class, | |
LanguageKeyErrorException::class, | |
NotFoundHttpException::class, | |
TokenMismatchException::class | |
]; | |
/** | |
* 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) | |
{ | |
switch (true) { | |
case $exception instanceof FormInputOutdatedException: | |
return $this->withMessage(trans('miscellaneous.outdated'), 'warning', null, 422); | |
case $exception instanceof LanguageKeyErrorException: | |
$lang = Lang::get($exception->getLanguageKey(), $exception->getParameters()); | |
return $this->withMessage($lang, 'error'); | |
case $exception instanceof HttpException: | |
return $this->handleHttpException($request, $exception); | |
} | |
return parent::render($request, $exception); | |
} | |
/** | |
* Manages the various HTTP exceptions specific to the application. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param HttpException $exception | |
* @return Response | |
*/ | |
private function handleHttpException($request, HttpException $exception) | |
{ | |
$this->log->notice("Caught HttpException for [{$exception->getStatusCode()}] {$request->fullUrl()}", [ | |
'fullUrl' => $request->fullUrl(), | |
'status' => $exception->getStatusCode(), | |
'message' => $exception->getMessage(), | |
'headers' => $exception->getHeaders(), | |
]); | |
$this->setupLayout(); | |
switch ($exception->getStatusCode()) { | |
case 404: | |
case 403: | |
case 401: | |
$this->respond('errors.'.$exception->getStatusCode()); | |
return new Response($this->layout, $exception->getStatusCode()); | |
} | |
return parent::render($request, $exception); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment