ルーメン5.4。LumenのAfterMiddleware内の例外を捕まえるには?
/**
* Get the initial slice to begin the stack call.
*
* @param \Closure $destination
* @return \Closure
*/
protected function prepareDestination(BaseClosure $destination)
{
return function ($passable) use ($destination) {
try {
return call_user_func($destination, $passable);
} catch (Exception $e) {
return $this->handleException($passable, $e);
} catch (Throwable $e) {
return $this->handleException($passable, new FatalThrowableError($e));
}
};
}
をそして、それはそこにcatchedだので、私は私のAfterMiddleware
は無用です:例外が発生した後
class AfterMiddleware
{
public function handle($request, Closure $next)
{
try {
return $next($request);
} catch (IpValidationException $e) {
return response()->json($e->getMessage(), 422);
} catch (RemoteException $e) {
return response()->json($e->getMessage(), 503);
} catch (BaseException $e) {
return response()->json($e->getMessage(), 400);
} catch (\Exception $e) {
return response()->json($e->getMessage(), $e->getCode());
}
}
}
、$next($request)
はLaravel\Lumen\Routing
に次の関数になります。どのようにそれを回避するための任意のアイデア?解決策を見つけて、すべての例外をHandler
クラスのrender()
に移動しましたが、ミドルウェアを使用する方がはるかに便利です。
私はこれを逃しました。私が今ハンドラクラスで必要なロジックを持っていた唯一の問題は、これにもかかわらず、あなたの解決策を知ることです! – wujt
いくつかの考えの後、あなたがExceptionHandlerなしで滞在するなら、それは良い考えではないようです。したがって、これを使用しないことが最良のアイデアではないため、他のいくつかの例外は自動的に処理されません。 – wujt