[英]Laravel PHP cannot get try-catch to work with DB updated
我有一個視頻文件可以使用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行返回錯誤(請參見上面的注釋),並且數據庫未更新:(
似乎在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();
}
並且更建議您捕獲throwables
( PHP: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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.