数据库事务可能会造成未知的错误

bug问题 · sumic · 于 5年前 发布 · 1862 次阅读

service articlemysql

try {
                foreach ($ids as $id) {
                    $model = $this->_articleModel->findOne($id);
                    if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
                        $url_key = $model['url_key'];
                        Yii::$service->url->removeRewriteUrlKey($url_key);
                        $model->delete();
                    } else {

                        //throw new InvalidValueException("ID:$id is not exist.");
                        Yii::$service->helper->errors->add("Article Remove Errors:ID $id is not exist.");
                        $innerTransaction->rollBack();

                        return false;
                    }
                }
                $innerTransaction->commit();
            } catch (Exception $e) {
                Yii::$service->helper->errors->add('Article Remove Errors: transaction rollback');
                $innerTransaction->rollBack();

                return false;
            }

问题1;catch捕获异常方法调用错误 问题2;catch捕获异常在PHP7版本改成了throw 更正代码如下

try {
                foreach ($ids as $id) {
                    $model = $this->_articleModel->findOne($id);
                    if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
                        $url_key = $model['url_key'];
                        Yii::$service->url->removeRewriteUrlKey($url_key);
                        $model->delete();
                    } else {

                        //throw new InvalidValueException("ID:$id is not exist.");
                        Yii::$service->helper->errors->add("Article Remove Errors:ID $id is not exist.");
                        $innerTransaction->rollBack();

                        return false;
                    }
                }
                $innerTransaction->commit();
            } catch (\Exception $e) {
                Yii::$service->helper->errors->add('Article Remove Errors: transaction rollback');
                $innerTransaction->rollBack();

                return false;
            } catch (\Throwable $e) {
            Yii::$service->helper->errors->add('Article Remove Errors: transaction rollback');
            $innerTransaction->rollBack();
            
            return false;
            }
共收到 5 条回复
Terry#15年前 0 个赞

如果报错是什么?写一下

你的意思是: 出了捕捉 \Exception $e , 还需要捕捉 \Throwable $e ?

\Throwable $e 是 php7独有的?还是?

sumic#25年前 0 个赞

其实影响不大,异常这块也没有做相应的抛出消息处理,只是正好看到这块。

Terry#35年前 0 个赞

@sumic #2楼 找资料看了看

https://segmentfault.com/q/1010000012522484

Throwable = Exception + Error

Throwable 是 php7 新增的顶级异常 interface,包含了 Error 和 Exception。

Throwable is the base interface for any object that can be thrown via a throw statement in PHP 7, including Error and Exception. PHP classes cannot implement the Throwable interface directly, and must instead extend Exception.

Terry#45年前 0 个赞

PHP中将代码自身异常(一般是环境或者语法非法所致)称作错误Error,将运行中出现的逻辑错误称为异常Exception。

错误是没法通过代码处理的,而异常则可以通过try..catch来处理;

PHP7中出现了Throwable接口,该接口由Error和Exception实现,用户不能直接实现Throwable接口,而只能通过继承Exception来实现接口;**

Terry#55年前 0 个赞

对于出现Error,直接报错,推出,也是正常的(php7当然更强,可以处理Error)

对于出现的Error,Fecshop 有ErrHandler机制,可以记录下来的,参看文档:http://www.fecshop.com/doc/fecshop-guide/instructions/cn-1.0/guide-fecshop_error_handler.html

记录下来,在后台可以查看Error,详细您,参看上面的文档

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics