laravel无法处理异常,catch块没有执行

问题描述 投票:2回答:1

我正在创建一个在远程服务器上执行各种操作的SSH Bot。这些服务器的用户名和密码由用户提供。

但是当用户提供错误的用户名或密码或服务器的错误IP地址时,它会抛出ErrorException异常。

我想处理该异常并在数据库中存储错误消息。而不是向用户显示它。

这是我的代码

try {
                $pending_server->console_output = $this->setupDNS($pending_server->toArray());
                $pending_server->status = 'Configured';
            } catch (ErrorException $e) {
                $pending_server->console_output = $e->getMessage;
                $pending_server->status = 'Failed';
                $pending_server->save();
            }

在数据库中,$pending_server是数据库模型。 setupDNS方法抛出异常。因此,如果出现异常,我希望将错误消息存储为数据库中的输出。但是catch块根本没有执行并且脚本停止执行。

php exception-handling laravel-5.6
1个回答
2
投票

只需在ErrorException之前输入'\'。例如:

try {
                $pending_server->console_output = $this->setupDNS($pending_server->toArray());
                $pending_server->status = 'Configured';
            } catch (\ErrorException $e) {
                $pending_server->console_output = $e->getMessage;
                $pending_server->status = 'Failed';
                $pending_server->save();
            }
© www.soinside.com 2019 - 2024. All rights reserved.