1. Go to this page and download the library: Download tounaf/exception-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tounaf / exception-bundle example snippets
namespace App\Handler\Exception;
class MyException extends \Exception
{
}
namespace App\Handler\Exception;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Tounaf\ExceptionBundle\ExceptionHandlerInterface;
class MyExceptionHandler implements ExceptionHandlerInterface
{
// return an JsonResponse
public function handleException(\Throwable $throwable): Response
{
// your logic
return new JsonResponse(['message' => $throwable->getMessage(), 'code' => 12]);
}
//
public function supportsException(\Throwable $throwable): Response
{
return $throwable instanceof MyException;
}
}
namespace App\Service;
use App\Handler\Exception\MyException;
class MyService
{
public function someFunction()
{
// your logic
throw new MyException();
}
}
/api/users/list returns a json {"data": [{"name":"user1"}, {"name":"user2"}]}
/admin/users/list renders an html page
namespace App\Handler\Exception;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Tounaf\ExceptionBundle\ExceptionHandlerInterface;
use Tounaf\ExceptionBundle\FormatResponse\FormatResponseCheckerInterface;
class MyExceptionHandler implements ExceptionHandlerInterface, FormatResponseCheckerInterface
{
private FormatResponseInterface $formatResponse;
// return an JsonResponse
public function handleException(\Throwable $throwable): Response
{
// your logic
return $this->formatResponse->render(
['message' => $throwable->getMessage()]
)
}
//
public function supportsException(\Throwable $throwable): Response
{
return $throwable instanceof MyException;
}
public function setFormat(FormatResponseInterface $formatResponse): void
{
$this->formatResponse = $formatResponse;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.