yii2:正确的方法来抛出新的异常 [英] yii2: Proper way to throw new exception

查看:407
本文介绍了yii2:正确的方法来抛出新的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  if($包装日期< 1)
{
throw new \yii\base\Exception(package days不能小于1);
}

现在Yii调试是真的:我得到



异常 - yii\base\Exception
包装日不能小于1



但是当我将调试设置为false
我得到
异常
发生内部服务器错误。



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



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



谢谢。

解决方案

你应该简单地抛出一个 UserException


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


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


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

if($packagedays < 1)
     {
      throw new \yii\base\Exception( "package days cannot be less than 1" );
     }

Now when Yii debug is true: I am getting

Exception – yii\base\Exception package days cannot be less than 1

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

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

What I want is to replace the An internal server error occurred. with package days cannot be less than 1 when debug=false

What I am missing here?

Thanks.

解决方案

You should simply throw a UserException :

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

Read more : http://www.yiiframework.com/doc-2.0/yii-base-userexception.html

这篇关于yii2:正确的方法来抛出新的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆