简体   繁体   中英

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.

The reason We are hitting multiple url to fetch the data and if there is any exception occurred whole processing get stopped.

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?

That way your exception listener will never trigger and you can handle the exception differently in that specific part of code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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