]> BookStack Code Mirror - bookstack/blob - app/Exceptions/Handler.php
Merge branch 'master' of git://github.com/lommes/BookStack into lommes-master
[bookstack] / app / Exceptions / Handler.php
1 <?php
2
3 namespace BookStack\Exceptions;
4
5 use Exception;
6 use Illuminate\Auth\AuthenticationException;
7 use Illuminate\Validation\ValidationException;
8 use Illuminate\Database\Eloquent\ModelNotFoundException;
9 use Symfony\Component\HttpKernel\Exception\HttpException;
10 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
11 use Illuminate\Auth\Access\AuthorizationException;
12
13 class Handler extends ExceptionHandler
14 {
15     /**
16      * A list of the exception types that should not be reported.
17      *
18      * @var array
19      */
20     protected $dontReport = [
21         AuthorizationException::class,
22         HttpException::class,
23         ModelNotFoundException::class,
24         ValidationException::class,
25     ];
26
27     /**
28      * Report or log an exception.
29      * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
30      *
31      * @param  \Exception $e
32      * @return mixed
33      */
34     public function report(Exception $e)
35     {
36         return parent::report($e);
37     }
38
39     /**
40      * Render an exception into an HTTP response.
41      *
42      * @param  \Illuminate\Http\Request $request
43      * @param  \Exception $e
44      * @return \Illuminate\Http\Response
45      */
46     public function render($request, Exception $e)
47     {
48         // Handle notify exceptions which will redirect to the
49         // specified location then show a notification message.
50         if ($this->isExceptionType($e, NotifyException::class)) {
51             session()->flash('error', $this->getOriginalMessage($e));
52             return redirect($e->redirectLocation);
53         }
54
55         // Handle pretty exceptions which will show a friendly application-fitting page
56         // Which will include the basic message to point the user roughly to the cause.
57         if ($this->isExceptionType($e, PrettyException::class)  && !config('app.debug')) {
58             $message = $this->getOriginalMessage($e);
59             $code = ($e->getCode() === 0) ? 500 : $e->getCode();
60             return response()->view('errors/' . $code, ['message' => $message], $code);
61         }
62
63         return parent::render($request, $e);
64     }
65
66     /**
67      * Check the exception chain to compare against the original exception type.
68      * @param Exception $e
69      * @param $type
70      * @return bool
71      */
72     protected function isExceptionType(Exception $e, $type) {
73         do {
74             if (is_a($e, $type)) return true;
75         } while ($e = $e->getPrevious());
76         return false;
77     }
78
79     /**
80      * Get original exception message.
81      * @param Exception $e
82      * @return string
83      */
84     protected function getOriginalMessage(Exception $e) {
85         do {
86             $message = $e->getMessage();
87         } while ($e = $e->getPrevious());
88         return $message;
89     }
90
91     /**
92      * Convert an authentication exception into an unauthenticated response.
93      *
94      * @param  \Illuminate\Http\Request  $request
95      * @param  \Illuminate\Auth\AuthenticationException  $exception
96      * @return \Illuminate\Http\Response
97      */
98     protected function unauthenticated($request, AuthenticationException $exception)
99     {
100         if ($request->expectsJson()) {
101             return response()->json(['error' => 'Unauthenticated.'], 401);
102         }
103
104         return redirect()->guest('login');
105     }
106
107     /**
108      * Convert a validation exception into a JSON response.
109      *
110      * @param  \Illuminate\Http\Request  $request
111      * @param  \Illuminate\Validation\ValidationException  $exception
112      * @return \Illuminate\Http\JsonResponse
113      */
114     protected function invalidJson($request, ValidationException $exception)
115     {
116         return response()->json($exception->errors(), $exception->status);
117     }
118 }