ExceptionHandler.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\ExceptionHandler;
  12. use Swow\Psr7\Message\ResponsePlusInterface;
  13. use Throwable;
  14. abstract class ExceptionHandler
  15. {
  16. /**
  17. * Handle the exception, and return the specified result.
  18. */
  19. abstract public function handle(Throwable $throwable, ResponsePlusInterface $response);
  20. /**
  21. * Determine if the current exception handler should handle the exception.
  22. *
  23. * @see ExceptionHandler::stopPropagation() if you want to stop propagation after handling
  24. * an exception, as returning `true` in `isValid` does not stop the handlers call loop.
  25. *
  26. * @return bool If return true, then this exception handler will handle the exception and then call the next handler,
  27. * If return false, this handler will be ignored and the next will be called
  28. */
  29. abstract public function isValid(Throwable $throwable): bool;
  30. /**
  31. * Stop propagate the exception to next handler.
  32. */
  33. public function stopPropagation(): bool
  34. {
  35. Propagation::instance()->setPropagationStopped(true);
  36. return true;
  37. }
  38. /**
  39. * Is propagation stopped ?
  40. * This will typically only be used by the handler to determine if the
  41. * previous handler halted propagation.
  42. */
  43. public function isPropagationStopped(): bool
  44. {
  45. return Propagation::instance()->isPropagationStopped();
  46. }
  47. }