Laravel PHP无法获取try-catch以更新数据库

时间:2018-12-15 13:04:50

标签: php exception exception-handling

我有一个视频文件可以使用FFPROBE进行探测,我正在尝试捕获错误,以便它首先抛出DB行并将其设置为状态2(已处理0 =默认,已处理0 =完成,已处理2 =错误)。

我先尝试过这个:

$user     = Auth::user()->id;
$video    = Video::find($videoUploaded->video->id);
$playlist = $video->playlist->id;

...

try {
     //Line 39 $seconds
    $seconds = $ffprobe->format(config('cf.video_disk') . "/$user/$playlist/$video->ts_name")->get('duration');     
} catch (\Exeption $err) {
    $video->processed = 2;
    $video->name      = $err->getMessage();
    $video->url       = $err->getMessage();
    $video->update();
    event(new VideoUpdated($video));
    return $err->getMessage();
}

并使用@抑制错误,并尝试尝试移动数据库更新:

try {
    //Line 39 $seconds
    $seconds = @$ffprobe->format(config('wondermark.video_disk') . "/$user/$playlist/$video->ts_name")->get('duration'); //Line 39
    if (FALSE === $seconds) {
        $video->processed = 2;
        $video->name      = $err->getMessage();
        $video->url       = $err->getMessage();
        $video->update();
    }
} catch (\Exeption $err) {
    event(new VideoUpdated($video));
    return $err->getMessage();
}

都在第39行返回错误(请参见上面的注释),并且数据库未更新:(

1 个答案:

答案 0 :(得分:1)

看来Exception的拼写只有一个错误,所以我想这会起作用:

try {
    $seconds = $ffprobe->format(config('cf.video_disk') . "/$user/$playlist/$video->ts_name")->get('duration');     
    // if no errors
} catch (\Exception $err) {
    // if error happens
    return $err->getMessage();
}

更建议您抓住throwablesPHP: Throwable - Manual)而不是exceptions

try {
    $seconds = $ffprobe->format(config('cf.video_disk') . "/$user/$playlist/$video->ts_name")->get('duration');     
    // if no errors
} catch (\Throwable $throwable) {
    // if error happens
    return $throwable->getMessage();
}