gpt4 book ai didi

php - 阻止 Laravel 在异常中包含跟踪

转载 作者:搜寻专家 更新时间:2023-10-31 21:30:58 24 4
gpt4 key购买 nike

我有自己的异常扩展,调用时我希望它向浏览器输出一条消息。然而,当我抛出这个异常时,捕获它并输出消息但消息是一个痕迹。

这是我的 global.php:

class ApiException extends Exception {}
App::error(function(ApiException $ex){
dd($ex->getMessage());
});

我的代码片段:

try {
if (!Input::get('password')) {
throw new Exception('Password not set');
}
if (User::all()->count()) {
throw new Exception('User already exists');
}
Artisan::call('db:seed', [
'--class' => 'VerifyUserSeeder'
]);
$User = \Toddish\Verify\Models\User::find(1);
$User->password = Input::get('password');
$User->save();
} catch (Exception $ex) {
throw new ApiException($ex);
}

输出到浏览器:

exception 'Exception' with message 'Password not set' in /Users/kevin/Documents/Web/app/controllers/Settings/SetupController.php:8 Stack trace: #0 [internal function]: SetupController->setupPassword() 1 /Users/kevin/Documents/Web/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(231): call_user_func_array(Array, Array) #2 .......

最佳答案

您能解释一下为什么要抛出一般异常、捕获它们然后再次抛出新异常吗?

为什么不直接抛出 ApiException 呢?

if (!Input::get('password')) {
throw new ApiException('Password not set');
}
if (User::all()->count()) {
throw new ApiException('User already exists');
}
Artisan::call('db:seed', [
'--class' => 'VerifyUserSeeder'
]);
$User = \Toddish\Verify\Models\User::find(1);
$User->password = Input::get('password');
$User->save();

关于php - 阻止 Laravel 在异常中包含跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30119925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com