【问题标题】:Laravel exceptions handlerLaravel 异常处理程序
【发布时间】:2016-07-25 07:33:06
【问题描述】:

我正在使用 Laravel 开发一个项目,异常在渲染函数内的 Exceptions\Handler.php 中被捕获,如下所示:

public function render($request, Exception $e){
      switch(get_class($e)){
              case SOME_EXCEPTION::class:
                    do something..
              ...
              ...
              default:
                    do something..
     }

问题如你所见,很多情况下代码变得丑陋和混乱

如何解决这个问题?

【问题讨论】:

  • 删除了含糊不清或主要基于意见的内容(例如最佳实践)

标签: php laravel exception-handling laravel-5.1


【解决方案1】:

如果您的自定义异常扩展了一个通用接口,您可以只检查该接口,然后调用合约方法。

if ($e instanceof CustomExceptionInterface) {
    return $e->contractMethod();
}

【讨论】:

    【解决方案2】:

    好的,找到了让它看起来更好的方法。 如果有人想在 laravel 中改进他的异常处理程序,请按照以下步骤操作:

    在 app/providers 下创建你的新服务提供者,我们称之为 ExceptionServiceProvider.php

        class ExceptionServiceProvider extends ServiceProvider {
    
        /**
         * Register the service provider.
         *
         * @return void
         */
        public function register()
        {
            $this->app->singleton(ExceptionFactory::class);
        }
    
        public function boot(ExceptionFactory $factory){
            $factory->addException(UnauthorizedException::class, JsonResponse::HTTP_NOT_ACCEPTABLE);
            $factory->addException(ConditionException::class, JsonResponse::HTTP_NOT_ACCEPTABLE, "Some Fixed Error Message");
    
        }
    }
    

    在项目的某处创建 ExceptionFactory 类,其中包含 addException() 方法和代码和消息的 getter

    class ExceptionFactory{
    
    
    private $exceptionsMap = [];
    private $selectedException;
    
    public function addException($exception, $code, $customMessage = null) {
        $this->exceptionsMap[$exception] = [$code, $customMessage];
    }
    
    public function getException($exception){
        if(isset($this->exceptionsMap[$exception])){
            return $this->exceptionsMap[$exception];
        }
        return null;
    }
    
    public function setException($exception){
        $this->selectedException = $exception;
    }
    
    public function getCode(){
        return $this->selectedException[0];
    }
    
    public function getCustomMessage(){
        return $this->selectedException[1];
    }
    

    }

    那么剩下要做的就是在Exceptions/handler.php 在渲染函数中:

    private $exceptionFactory;
    
        public function __construct(LoggerInterface $log, ExceptionFactory $exceptionFactory){
            parent::__construct($log);
            $this->exceptionFactory = $exceptionFactory;
        }
    
    public function render($request, Exception $e){
            $error = new \stdClass();
            $customException = $this->exceptionFactory->getException(get_class($e));
    
            if(isset($customException)){
                $this->exceptionFactory->setException($customException);
                $error->code = $this->exceptionFactory->getCode();
                $error->message = $e->getMessage();
                $customMessage = $this->exceptionFactory->getCustomMessage();
                if(isset($customMessage)){
                    $error->message = $customMessage;
                }
           }
           return new JsonResponse($error, $error->code);
     }
    }
    

    最后要记住的是将ServiceProvider 放在config/app.php 下的应用程序设置中,只需添加:

    \App\Providers\ExceptionServiceProvider::class
    

    我希望你会像我一样发现这很有用。

    【讨论】:

      猜你喜欢
      • 2015-09-03
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      • 2015-05-07
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多