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