PageRenderTime 61ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Exceptions/Handler.php

https://bitbucket.org/dothessay/fanaka
PHP | 163 lines | 83 code | 40 blank | 40 comment | 14 complexity | 46e96931ca2b724f1562ce01d6acc74b MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. namespace App\Exceptions;
  3. use Exception;
  4. use HttpException;
  5. use Illuminate\Auth\AuthenticationException;
  6. use Illuminate\Database\QueryException;
  7. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  8. use Illuminate\Support\Facades\Mail;
  9. use Illuminate\Http\Response;
  10. use Symfony\Component\Debug\Exception\FatalThrowableError;
  11. use Illuminate\Http\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
  13. use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirectResponse;
  14. class Handler extends ExceptionHandler
  15. {
  16. /**
  17. * A list of the exception types that are not reported.
  18. *
  19. * @var array
  20. */
  21. protected $dontReport = [
  22. //
  23. ];
  24. /**
  25. * A list of the inputs that are never flashed for validation exceptions.
  26. *
  27. * @var array
  28. */
  29. protected $dontFlash = [
  30. 'password',
  31. 'password_confirmation',
  32. ];
  33. /**
  34. * Report or log an exception.
  35. *
  36. * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
  37. *
  38. * @param \Exception $exception
  39. * @return void
  40. */
  41. public function report(Exception $exception)
  42. {
  43. parent::report($exception);
  44. }
  45. /**
  46. * Render an exception into an HTTP response.
  47. *
  48. * @param \Illuminate\Http\Request $request
  49. * @param \Exception $exception
  50. * @return \Illuminate\Http\Response
  51. */
  52. /**
  53. * Render an exception into an HTTP response.
  54. *
  55. * @param \Illuminate\Http\Request $request
  56. * @param \Exception $exception
  57. * @return \Illuminate\Http\Response
  58. */
  59. public function render($request, Exception $exception)
  60. {
  61. return parent::render($request, $exception);
  62. }
  63. /**
  64. * Prepare response containing exception render. {{IMPORT FUNCTION FROM LARAVEL 5.5 Master}}
  65. *
  66. * @param \Illuminate\Http\Request $request
  67. * @param \Exception $e
  68. * @return \Symfony\Component\HttpFoundation\Response
  69. */
  70. protected function prepareResponse($request, Exception $e)
  71. {
  72. if (env('APP_DEBUG') ){
  73. dd($e);
  74. }
  75. $response = $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e);
  76. if ($response->getStatusCode() == 419)
  77. {
  78. return redirect()->refresh();
  79. }
  80. $message = "";
  81. if ($e->getMessage() != 'Call to a member function setCookie() on null' &&
  82. $e->getMessage() != "Argument 1 passed to Illuminate\Session\Middleware\StartSession::addCookieToResponse() must be an instance of Symfony\Component\HttpFoundation\Response, instance of Illuminate\View\View given, called in /var/www/html/fanaka/fanaka/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php on line 71"
  83. && $e->getMessage() != "Argument 1 passed to Illuminate\Cookie\Middleware\EncryptCookies::encrypt() must be an instance of Symfony\Component\HttpFoundation\Response, instance of Illuminate\View\View given, called in /var/www/html/fanaka/fanaka/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php on line 66")
  84. {
  85. $message = $e->getMessage();
  86. Mail::raw( json_encode($e), function ($mail) use ($e , $message) {
  87. $mail->to('philipnjuguna66@gmail.com')
  88. ->subject($message. ' on '. $e->getLine() .' of '. $e->getFile(). 'User: '. request()->user());
  89. });
  90. }
  91. if ($response->getStatusCode() === 417){
  92. return view('errors.417');
  93. }
  94. if (!$this->isHttpException($e) && config('app.debug')) {
  95. return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e);
  96. }
  97. if (!$this->isHttpException($e)) {
  98. $e = new HttpException(422, $e->getMessage());
  99. }
  100. return $this->toIlluminateResponse($this->renderHttpException($e), $e);
  101. }
  102. protected function toIlluminateResponse($response, Exception $e)
  103. {
  104. if ($response instanceof SymfonyRedirectResponse) {
  105. $response = new RedirectResponse(
  106. $response->getTargetUrl(), $response->getStatusCode(), $response->headers->all()
  107. );
  108. } else {
  109. $response = new Response(
  110. $response->getContent(), $response->getStatusCode(), $response->headers->all()
  111. );
  112. }
  113. return $response->withException($e);
  114. }
  115. protected function convertExceptionToResponse(Exception $e)
  116. {
  117. return SymfonyResponse::create(
  118. $this->renderExceptionContent($e),
  119. $this->isHttpException($e) ? $e->getStatusCode() : 417,
  120. $this->isHttpException($e) ? $e->getHeaders() : []
  121. );
  122. }
  123. }