[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Error/JsonApi/ -> ResourceNotFoundExceptionHandler.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  namespace Joomla\CMS\Error\JsonApi;
  11  
  12  use Exception;
  13  use Joomla\CMS\MVC\Controller\Exception\ResourceNotFound;
  14  use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
  15  use Tobscure\JsonApi\Exception\Handler\ResponseBag;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('_JEXEC') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Handler for invalid resource requests that should give a 404
  23   *
  24   * @since  4.0.0
  25   */
  26  class ResourceNotFoundExceptionHandler implements ExceptionHandlerInterface
  27  {
  28      /**
  29       * If the exception handler is able to format a response for the provided exception,
  30       * then the implementation should return true.
  31       *
  32       * @param   \Exception  $e  The exception to be handled
  33       *
  34       * @return boolean
  35       *
  36       * @since  4.0.0
  37       */
  38      public function manages(Exception $e)
  39      {
  40          return $e instanceof ResourceNotFound;
  41      }
  42  
  43      /**
  44       * Handle the provided exception.
  45       *
  46       * @param   Exception  $e  The exception being handled
  47       *
  48       * @return  \Tobscure\JsonApi\Exception\Handler\ResponseBag
  49       *
  50       * @since  4.0.0
  51       */
  52      public function handle(Exception $e)
  53      {
  54          $status = 404;
  55          $error = ['title' => 'Resource not found'];
  56  
  57          $code = $e->getCode();
  58  
  59          if ($code) {
  60              $error['code'] = $code;
  61          }
  62  
  63          return new ResponseBag($status, [$error]);
  64      }
  65  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer