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