]> BookStack Code Mirror - bookstack/blob - app/Exceptions/Handler.php
Laravel 8 shift squash & merge (#3029)
[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 use Throwable;
13
14 class Handler extends ExceptionHandler
15 {
16     /**
17      * A list of the exception types that are not reported.
18      *
19      * @var array
20      */
21     protected $dontReport = [
22         NotFoundException::class,
23     ];
24
25     /**
26      * A list of the inputs that are never flashed for validation exceptions.
27      *
28      * @var array
29      */
30     protected $dontFlash = [
31         'current_password',
32         'password',
33         'password_confirmation',
34     ];
35
36     /**
37      * Report or log an exception.
38      *
39      * @param \Throwable $exception
40      *
41      * @throws \Throwable
42      *
43      * @return void
44      */
45     public function report(Throwable $exception)
46     {
47         parent::report($exception);
48     }
49
50     /**
51      * Render an exception into an HTTP response.
52      *
53      * @param \Illuminate\Http\Request $request
54      * @param Exception                $e
55      *
56      * @return \Illuminate\Http\Response
57      */
58     public function render($request, Throwable $e)
59     {
60         if ($this->isApiRequest($request)) {
61             return $this->renderApiException($e);
62         }
63
64         return parent::render($request, $e);
65     }
66
67     /**
68      * Check if the given request is an API request.
69      */
70     protected function isApiRequest(Request $request): bool
71     {
72         return strpos($request->path(), 'api/') === 0;
73     }
74
75     /**
76      * Render an exception when the API is in use.
77      */
78     protected function renderApiException(Exception $e): JsonResponse
79     {
80         $code = $e->getCode() === 0 ? 500 : $e->getCode();
81         $headers = [];
82         if ($e instanceof HttpException) {
83             $code = $e->getStatusCode();
84             $headers = $e->getHeaders();
85         }
86
87         $responseData = [
88             'error' => [
89                 'message' => $e->getMessage(),
90             ],
91         ];
92
93         if ($e instanceof ValidationException) {
94             $responseData['error']['validation'] = $e->errors();
95             $code = $e->status;
96         }
97
98         $responseData['error']['code'] = $code;
99
100         return new JsonResponse($responseData, $code, $headers);
101     }
102
103     /**
104      * Convert an authentication exception into an unauthenticated response.
105      *
106      * @param \Illuminate\Http\Request                 $request
107      * @param \Illuminate\Auth\AuthenticationException $exception
108      *
109      * @return \Illuminate\Http\Response
110      */
111     protected function unauthenticated($request, AuthenticationException $exception)
112     {
113         if ($request->expectsJson()) {
114             return response()->json(['error' => 'Unauthenticated.'], 401);
115         }
116
117         return redirect()->guest('login');
118     }
119
120     /**
121      * Convert a validation exception into a JSON response.
122      *
123      * @param \Illuminate\Http\Request                   $request
124      * @param \Illuminate\Validation\ValidationException $exception
125      *
126      * @return \Illuminate\Http\JsonResponse
127      */
128     protected function invalidJson($request, ValidationException $exception)
129     {
130         return response()->json($exception->errors(), $exception->status);
131     }
132 }