Reputation: 21
I'm working on a PHP/Lumen 8.x foreach loop to manage the update/insert of records from a mysql DB table in which I'm trying to catch any errors during the update/insert process so that it can continue with subsequent logs in case of error. As I am with Lumen and we are in a certain namespace environment in the catch statement I use "\Exception" to identify the type of exception. However, in no case in which an error occurs can I catch it and in the controller output, that is, in the controller response, an error indicating "Recursion detected" is triggered. I can't catch this last error either.
This is the code snippet for the foreach(...) { ... try..catch ...} block:
foreach($prdsData as $prod){
$cr_sku = $prod['SKU'];
try{
$res_prod_uc[$cr_sku]['err'] = false;
$res_prod_uc[$cr_sku]['res'] = wc_product_update($pd);
} catch( \Exception $e ){
$res_prod_uc[$cr_sku]['err'] = true;
$cr_err_dt = array(
'code' => $e->getCode(),
'msg' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTrace()
);
$res_prod_uc[$cr_sku]['err_data'] = $cr_err_dt;
continue;
}
}
return response()->json($res_prod_uc,200); // this is where the "Recursion detected" error is triggered
This is the output (JSON) of the "Recursion detected" error:
{
"message": "Recursion detected",
"exception": "InvalidArgumentException",
"file": "..../blog/vendor/illuminate/http/JsonResponse.php",
"line": 88,
"trace": [
{
"file": "..../blog/vendor/symfony/http-foundation/JsonResponse.php",
"line": 54,
"function": "setData",
"class": "Illuminate\\Http\\JsonResponse",
"type": "->"
},
{
"file": "..../blog/vendor/illuminate/http/JsonResponse.php",
"line": 32,
"function": "__construct",
"class": "Symfony\\Component\\HttpFoundation\\JsonResponse",
"type": "->"
},
{
"file": "..../blog/vendor/laravel/lumen-framework/src/Http/ResponseFactory.php",
"line": 40,
"function": "__construct",
"class": "Illuminate\\Http\\JsonResponse",
"type": "->"
},
/* this is my controller */
{
"file": "..../blog/app/Http/Controllers/ProductsController.php",
"line": 145,
"function": "json",
"class": "Laravel\\Lumen\\Http\\ResponseFactory",
"type": "->"
},....
How can I go about catching any errors?
Thanks in advance
Upvotes: 1
Views: 750
Reputation: 21
What was happening is exactly what you say @aynber. I did check and, yes, exceptions are being cached, but the line 'trace' => $e->getTraceAsString()
does indeed allocate data where at deeper levels there is object recursion. And by the time the thread reaches return response()->json($res,200);
(Which is outside of the try..catch block) the "Recursion detected" error is raised. This error is generated by a call to the json_encode() PHP function found within the Lumen 8 framework.
The solution I used was to replace the getTrace() method with the getTraceAsString() Exception object method.
Upvotes: 1