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