Laravel PHP 无法让 try-catch 与更新的数据库一起工作
Laravel PHP cannot get try-catch to work with DB updated
我有一个视频文件要使用 FFPROBE 进行探测,我正在尝试捕获错误,这样它不仅会抛出错误,还会首先更新数据库行,将其设置为状态 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:
中移动数据库更新
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 行 return 错误(见上面的评论)和数据库都没有更新 :(
似乎只有拼写错误 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();
}
我有一个视频文件要使用 FFPROBE 进行探测,我正在尝试捕获错误,这样它不仅会抛出错误,还会首先更新数据库行,将其设置为状态 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:
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 行 return 错误(见上面的评论)和数据库都没有更新 :(
似乎只有拼写错误 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();
}