Moved NotifyException render work from handler to exception
As continued from last commit.
This commit is contained in:
		
							parent
							
								
									7be7d7d1e7
								
							
						
					
					
						commit
						9e7bcacf8c
					
				| 
						 | 
				
			
			@ -9,7 +9,6 @@ use Illuminate\Http\JsonResponse;
 | 
			
		|||
use Illuminate\Http\Request;
 | 
			
		||||
use Illuminate\Validation\ValidationException;
 | 
			
		||||
use Symfony\Component\HttpKernel\Exception\HttpException;
 | 
			
		||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 | 
			
		||||
 | 
			
		||||
class Handler extends ExceptionHandler
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -58,16 +57,6 @@ class Handler extends ExceptionHandler
 | 
			
		|||
            return $this->renderApiException($e);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Handle notify exceptions which will redirect to the
 | 
			
		||||
        // specified location then show a notification message.
 | 
			
		||||
        if ($this->isExceptionType($e, NotifyException::class)) {
 | 
			
		||||
            $message = $this->getOriginalMessage($e);
 | 
			
		||||
            if (!empty($message)) {
 | 
			
		||||
                session()->flash('error', $message);
 | 
			
		||||
            }
 | 
			
		||||
            return redirect($e->redirectLocation);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return parent::render($request, $e);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -106,30 +95,6 @@ class Handler extends ExceptionHandler
 | 
			
		|||
        return new JsonResponse($responseData, $code, $headers);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Check the exception chain to compare against the original exception type.
 | 
			
		||||
     */
 | 
			
		||||
    protected function isExceptionType(Exception $e, string $type): bool
 | 
			
		||||
    {
 | 
			
		||||
        do {
 | 
			
		||||
            if (is_a($e, $type)) {
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
        } while ($e = $e->getPrevious());
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get original exception message.
 | 
			
		||||
     */
 | 
			
		||||
    protected function getOriginalMessage(Exception $e): string
 | 
			
		||||
    {
 | 
			
		||||
        do {
 | 
			
		||||
            $message = $e->getMessage();
 | 
			
		||||
        } while ($e = $e->getPrevious());
 | 
			
		||||
        return $message;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Convert an authentication exception into an unauthenticated response.
 | 
			
		||||
     *
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,8 +1,10 @@
 | 
			
		|||
<?php namespace BookStack\Exceptions;
 | 
			
		||||
 | 
			
		||||
class NotifyException extends \Exception
 | 
			
		||||
{
 | 
			
		||||
use Exception;
 | 
			
		||||
use Illuminate\Contracts\Support\Responsable;
 | 
			
		||||
 | 
			
		||||
class NotifyException extends Exception implements Responsable
 | 
			
		||||
{
 | 
			
		||||
    public $message;
 | 
			
		||||
    public $redirectLocation;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -15,4 +17,19 @@ class NotifyException extends \Exception
 | 
			
		|||
        $this->redirectLocation = $redirectLocation;
 | 
			
		||||
        parent::__construct();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Send the response for this type of exception.
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    public function toResponse($request)
 | 
			
		||||
    {
 | 
			
		||||
        $message = $this->getMessage();
 | 
			
		||||
 | 
			
		||||
        if (!empty($message)) {
 | 
			
		||||
            session()->flash('error', $message);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return redirect($this->redirectLocation);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,7 +2,6 @@
 | 
			
		|||
 | 
			
		||||
use Exception;
 | 
			
		||||
use Illuminate\Contracts\Support\Responsable;
 | 
			
		||||
use Illuminate\Http\Request;
 | 
			
		||||
 | 
			
		||||
class PrettyException extends Exception implements Responsable
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -18,7 +17,7 @@ class PrettyException extends Exception implements Responsable
 | 
			
		|||
 | 
			
		||||
    /**
 | 
			
		||||
     * Render a response for when this exception occurs.
 | 
			
		||||
     * @param Request $request
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    public function toResponse($request)
 | 
			
		||||
    {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,7 +7,6 @@ use BookStack\Entities\Models\Page;
 | 
			
		|||
use BookStack\Entities\Repos\PageRepo;
 | 
			
		||||
use BookStack\Entities\Tools\PermissionsUpdater;
 | 
			
		||||
use BookStack\Exceptions\NotFoundException;
 | 
			
		||||
use BookStack\Exceptions\NotifyException;
 | 
			
		||||
use BookStack\Exceptions\PermissionsException;
 | 
			
		||||
use Exception;
 | 
			
		||||
use Illuminate\Http\Request;
 | 
			
		||||
| 
						 | 
				
			
			@ -295,7 +294,6 @@ class PageController extends Controller
 | 
			
		|||
     * Remove the specified page from storage.
 | 
			
		||||
     * @throws NotFoundException
 | 
			
		||||
     * @throws Throwable
 | 
			
		||||
     * @throws NotifyException
 | 
			
		||||
     */
 | 
			
		||||
    public function destroy(string $bookSlug, string $pageSlug)
 | 
			
		||||
    {
 | 
			
		||||
| 
						 | 
				
			
			@ -311,7 +309,6 @@ class PageController extends Controller
 | 
			
		|||
    /**
 | 
			
		||||
     * Remove the specified draft page from storage.
 | 
			
		||||
     * @throws NotFoundException
 | 
			
		||||
     * @throws NotifyException
 | 
			
		||||
     * @throws Throwable
 | 
			
		||||
     */
 | 
			
		||||
    public function destroyDraft(string $bookSlug, int $pageId)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue