]> BookStack Code Mirror - bookstack/blob - app/Exceptions/Handler.php
Apply fixes from StyleCI
[bookstack] / app / Exceptions / Handler.php
1 <?php
2
3 namespace BookStack\Exceptions;
4
5 use Exception;
6 use Illuminate\Auth\AuthenticationException;
7 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
8 use Illuminate\Http\JsonResponse;
9 use Illuminate\Http\Request;
10 use Illuminate\Validation\ValidationException;
11 use Symfony\Component\HttpKernel\Exception\HttpException;
12
13 class Handler extends ExceptionHandler
14 {
15     /**
16      * A list of the exception types that are not reported.
17      *
18      * @var array
19      */
20     protected $dontReport = [
21         NotFoundException::class,
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     /**
35      * Report or log an exception.
36      *
37      * @param Exception $exception
38      *
39      * @throws Exception
40      *
41      * @return void
42      */
43     public function report(Exception $exception)
44     {
45         parent::report($exception);
46     }
47
48     /**
49      * Render an exception into an HTTP response.
50      *
51      * @param \Illuminate\Http\Request $request
52      * @param Exception                $e
53      *
54      * @return \Illuminate\Http\Response
55      */
56     public function render($request, Exception $e)
57     {
58         if ($this->isApiRequest($request)) {
59             return $this->renderApiException($e);
60         }
61
62         return parent::render($request, $e);
63     }
64
65     /**
66      * Check if the given request is an API request.
67      */
68     protected function isApiRequest(Request $request): bool
69     {
70         return strpos($request->path(), 'api/') === 0;
71     }
72
73     /**
74      * Render an exception when the API is in use.
75      */
76     protected function renderApiException(Exception $e): JsonResponse
77     {
78         $code = $e->getCode() === 0 ? 500 : $e->getCode();
79         $headers = [];
80         if ($e instanceof HttpException) {
81             $code = $e->getStatusCode();
82             $headers = $e->getHeaders();
83         }
84
85         $responseData = [
86             'error' => [
87                 'message' => $e->getMessage(),
88             ],
89         ];
90
91         if ($e instanceof ValidationException) {
92             $responseData['error']['validation'] = $e->errors();
93             $code = $e->status;
94         }
95
96         $responseData['error']['code'] = $code;
97
98         return new JsonResponse($responseData, $code, $headers);
99     }
100
101     /**
102      * Convert an authentication exception into an unauthenticated response.
103      *
104      * @param \Illuminate\Http\Request                 $request
105      * @param \Illuminate\Auth\AuthenticationException $exception
106      *
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      *
124      * @return \Illuminate\Http\JsonResponse
125      */
126     protected function invalidJson($request, ValidationException $exception)
127     {
128         return response()->json($exception->errors(), $exception->status);
129     }
130 }