定制 webservco/exception 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

webservco/exception

最新稳定版本:v1.0.0

Composer 安装命令:

composer require webservco/exception

包简介

A PHP component/library.

README 文档

README

A PHP component/library.

Custom application exception handling.

Usage

Implement interfaces:

ExceptionHandlerFactoryInterface

interface ExceptionHandlerFactoryInterface
{
    public function createExceptionHandler(LoggerInterface $logger): ExceptionHandlerInterface;
}

ExceptionHandlerInterface

interface ExceptionHandlerInterface
{
    public function handle(Throwable $throwable): void;
}

UncaughtExceptionHandlerInterface

interface UncaughtExceptionHandlerInterface extends ExceptionHandlerInterface
{
}

Example implementation

Handle uncaught exceptions.

In application bootstrap:

// Exception handling.
$exceptionHandlerFactory = new DefaultExceptionHandlerFactory();
// Uncaught exception handler.
set_exception_handler([$exceptionHandlerFactory->createUncaughtExceptionHandler($logger), 'handle']);

Handle exceptions inside the application (caught exceptions)

In application logic:

$exceptionHandler = $exceptionHandlerFactory->createExceptionHandler($logger);

// Example: an exception handling middleware.
final class ExceptionHandlerMiddleware implements MiddlewareInterface
{
    public function __construct(
        // Exception handler to use to handle the exception.
        private ExceptionHandlerInterface $exceptionHandler,
        // Request handler to use to return a response to the client
        private RequestHandlerInterface $requestHandler,
    ) {
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        try {
            /**
             * Pass to the next handler.
             * If all is OK, nothing else to do.
             */
            return $handler->handle($request);
        } catch (Throwable $throwable) {
            /**
             * An exception happened inside one of the next handlers.
             */

            // Handle error (log, report, etc)
            $this->exceptionHandler->handle($throwable);

            /**
             * Return a response via the request handler.
             * Any exceptions that happen here will bubble up and be handled by the uncaught exception handler (if set).
             */
            return $this->requestHandler->handle($request->withAttribute('throwable', $throwable));
        }
    }
}

统计信息

  • 总下载量: 405
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固