]> BookStack Code Mirror - bookstack/blob - app/Exceptions/Handler.php
Merge branch 'master' of https://github.com/awarre/BookStack into awarre-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\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      * @return void
39      *
40      * @throws Exception
41      */
42     public function report(Exception $exception)
43     {
44         parent::report($exception);
45     }
46
47     /**
48      * Render an exception into an HTTP response.
49      *
50      * @param  \Illuminate\Http\Request $request
51      * @param Exception $e
52      * @return \Illuminate\Http\Response
53      */
54     public function render($request, Exception $e)
55     {
56         if ($this->isApiRequest($request)) {
57             return $this->renderApiException($e);
58         }
59
60         return parent::render($request, $e);
61     }
62
63     /**
64      * Check if the given request is an API request.
65      */
66     protected function isApiRequest(Request $request): bool
67     {
68         return strpos($request->path(), 'api/') === 0;
69     }
70
71     /**
72      * Render an exception when the API is in use.
73      */
74     protected function renderApiException(Exception $e): JsonResponse
75     {
76         $code = $e->getCode() === 0 ? 500 : $e->getCode();
77         $headers = [];
78         if ($e instanceof HttpException) {
79             $code = $e->getStatusCode();
80             $headers = $e->getHeaders();
81         }
82
83         $responseData = [
84             'error' => [
85                 'message' => $e->getMessage(),
86             ]
87         ];
88
89         if ($e instanceof ValidationException) {
90             $responseData['error']['validation'] = $e->errors();
91             $code = $e->status;
92         }
93
94         $responseData['error']['code'] = $code;
95         return new JsonResponse($responseData, $code, $headers);
96     }
97
98     /**
99      * Convert an authentication exception into an unauthenticated response.
100      *
101      * @param  \Illuminate\Http\Request  $request
102      * @param  \Illuminate\Auth\AuthenticationException  $exception
103      * @return \Illuminate\Http\Response
104      */
105     protected function unauthenticated($request, AuthenticationException $exception)
106     {
107         if ($request->expectsJson()) {
108             return response()->json(['error' => 'Unauthenticated.'], 401);
109         }
110
111         return redirect()->guest('login');
112     }
113
114     /**
115      * Convert a validation exception into a JSON response.
116      *
117      * @param  \Illuminate\Http\Request  $request
118      * @param  \Illuminate\Validation\ValidationException  $exception
119      * @return \Illuminate\Http\JsonResponse
120      */
121     protected function invalidJson($request, ValidationException $exception)
122     {
123         return response()->json($exception->errors(), $exception->status);
124     }
125 }