1 回答

TA貢獻1820條經驗 獲得超9個贊
在 Laravel 中,已經為您配置了錯誤和異常處理。無需使用自定義類來實現此目的。
所有異常均由該類處理App\Exceptions\Handler,您可以在該類上自定義方法report:render
/**
?* Render an exception into an HTTP response.
?*
?* @param? \Illuminate\Http\Request? $request
?* @param? \Throwable? $exception
?* @return \Illuminate\Http\Response
?*/
public function render($request, Throwable $exception)
{
? ? if ($exception instanceof TypeAException) {
? ? ? ? return response([
? ? ? ? ? ? 'message' => 'My custom message for Type A error',
? ? ? ? ? ? 'status' => 'Error',
? ? ? ? ? ? 'errors' => []
? ? ? ? ], 500);
? ? }
? ? else if ($exception instanceof TypeBException) {
? ? ? ? return response([
? ? ? ? ? ? 'message' => 'My custom message for Type B error',
? ? ? ? ? ? 'status' => 'Error',
? ? ? ? ? ? 'errors' => []
? ? ? ? ], 500);
? ? }
? ? return parent::render($request, $exception);
}
- 1 回答
- 0 關注
- 124 瀏覽
添加回答
舉報