简体   繁体   English

跳过symfony2内核异常

[英]skip symfony2 kernel exception

I am using symfony2.8 and We have a KernelExceptionService and I want to skip it if there is any Exception like 500,400 or any and get back to service and continue the work. 我正在使用symfony2.8,我们有一个KernelExceptionService,如果有任何异常(如500,400或任何异常),我想跳过它,然后恢复使用并继续工作。

The reason We are hitting multiple url to fetch the data and if there is any exception occurred whole processing get stopped. 我们点击多个URL来获取数据的原因,如果发生任何异常,整个处理将停止。

public function onKernelException(GetResponseForExceptionEvent $event) {
    $exception = $event->getException();
    $response = new JsonResponse;
    $request = $event->getRequest();

    if ($exception instanceof InvalidConfigurationException) {
        //500 case
        $responseData = return [
            'code' => Response::HTTP_NOT_FOUND,
            'message' => $exception->getMessage()
        ];
    } else {
       // same as aobve if with difference code
    }        

    //Prepare the response
    $response->setData($responseData);
    $response->setStatusCode($statusCode);
    $event->setResponse($response);
}

Just wrap the particular code with a try catch block? 只是用try catch块包装特定的代码?

That way your exception listener will never trigger and you can handle the exception differently in that specific part of code. 这样,您的异常侦听器将永远不会触发,并且您可以在代码的特定部分以不同的方式处理异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM