| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Exceptions; | 
					
						
							| 
									
										
										
										
											2015-09-05 00:16:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 02:00:09 +08:00
										 |  |  | use Exception; | 
					
						
							|  |  |  | use Illuminate\Contracts\Support\Responsable; | 
					
						
							| 
									
										
										
										
											2023-06-14 17:07:13 +08:00
										 |  |  | use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; | 
					
						
							| 
									
										
										
										
											2015-09-05 00:16:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 17:07:13 +08:00
										 |  |  | class NotifyException extends Exception implements Responsable, HttpExceptionInterface | 
					
						
							| 
									
										
										
										
											2021-05-09 02:00:09 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-05 00:16:58 +08:00
										 |  |  |     public $message; | 
					
						
							| 
									
										
										
										
											2023-06-16 00:07:40 +08:00
										 |  |  |     public string $redirectLocation; | 
					
						
							|  |  |  |     protected int $status; | 
					
						
							| 
									
										
										
										
											2023-06-14 17:07:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 08:26:19 +08:00
										 |  |  |     public function __construct(string $message, string $redirectLocation = '/', int $status = 500) | 
					
						
							| 
									
										
										
										
											2015-09-05 00:16:58 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->message = $message; | 
					
						
							|  |  |  |         $this->redirectLocation = $redirectLocation; | 
					
						
							| 
									
										
										
										
											2022-02-04 08:26:19 +08:00
										 |  |  |         $this->status = $status; | 
					
						
							| 
									
										
										
										
											2023-06-14 17:07:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-05 00:16:58 +08:00
										 |  |  |         parent::__construct(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-09 02:00:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 08:26:19 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-14 17:07:13 +08:00
										 |  |  |      * Get the desired HTTP status code for this exception. | 
					
						
							| 
									
										
										
										
											2022-02-04 08:26:19 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-14 17:07:13 +08:00
										 |  |  |     public function getStatusCode(): int | 
					
						
							| 
									
										
										
										
											2022-02-04 08:26:19 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         return $this->status; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 17:07:13 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the desired HTTP headers for this exception. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getHeaders(): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-16 00:07:40 +08:00
										 |  |  |         return []; | 
					
						
							| 
									
										
										
										
											2023-06-14 17:07:13 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 02:00:09 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Send the response for this type of exception. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-10-27 05:04:18 +08:00
										 |  |  |      * {@inheritdoc} | 
					
						
							| 
									
										
										
										
											2021-05-09 02:00:09 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function toResponse($request) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $message = $this->getMessage(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 09:02:13 +08:00
										 |  |  |         // Front-end JSON handling. API-side handling managed via handler.
 | 
					
						
							|  |  |  |         if ($request->wantsJson()) { | 
					
						
							| 
									
										
										
										
											2023-06-14 17:07:13 +08:00
										 |  |  |             return response()->json(['error' => $message], $this->getStatusCode()); | 
					
						
							| 
									
										
										
										
											2022-02-04 09:02:13 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 02:00:09 +08:00
										 |  |  |         if (!empty($message)) { | 
					
						
							|  |  |  |             session()->flash('error', $message); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return redirect($this->redirectLocation); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  | } |