我noticied,我經常寫這樣的事情Laravel抓PDOException/Handler.php
class SitesController
{
public function update(SiteRequest $request, Site $site)
{
try {
$site->update($request->all());
return redirect()->route('sites.index');
} catch (PDOException $e) {
return redirect()
->back()
->with('error', 'Cannot update site');
}
}
public function destroy(Site $site)
{
try {
$site->delete();
return redirect()->route('sites.index');
} catch (PDOException $e) {
return redirect()
->back()
->with('error', 'Cannot remove site');
}
}
}
我能趕上在app/Exceptions/Handler.php
這樣PDOException?想法不錯 ?有什麼缺點?有沒有辦法只捕獲從app/Http/Controllers文件夾中拋出 的PDOException?
// app/Exceptions/Handler.php
public function render($request, Exception $e)
{
if (!App::runningInConsole() && $e instanceof PDOException) {
return back()->with('error', 'Could not perform the operation');
}
return parent::render($request, $e);
}