yii2:抛出新异常的正确方法

2022-01-07 00:00:00 exception-handling php yii2

只是为了测试,我在我的模型中添加了这段代码,同时设置了 debug = true 和 false.

Just for testing I have added this code in my model while setting the debug = true and false.

if($packagedays < 1)
     {
      throw new yiiaseException( "package days cannot be less than 1" );
     }

现在当 Yii 调试为真时:我得到了

Now when Yii debug is true: I am getting

异常 – yiiaseException包裹天数不能少于 1

但是当我将调试设置为 false 时我正进入(状态例外发生内部服务器错误.

But when I am setting the debug to false I am getting Exception An internal server error occurred.

上述错误发生在 Web 服务器处理您的请求时.

The above error occurred while the Web server was processing your request.

我想要的是替换发生内部服务器错误.包天数不能小于1debug=false

我在这里缺少什么?

谢谢.

推荐答案

你应该简单地抛出一个 UserException :

You should simply throw a UserException :

UserException 是旨在向最终用户显示的异常的基类.

UserException is the base class for exceptions that are meant to be shown to end users.

阅读更多:http://www.yiiframework.com/doc-2.0/yii-base-userexception.html

相关文章