Nico Knoll Posted March 10, 2013 Share Posted March 10, 2013 Hey, I'm working on a console like module for processwire where the user can insert some code. But if the code fails I get a processwire exception. I tried: try { include $file; } catch (Exception $e) { echo 'Exception abgefangen: ', $e->getMessage(), "\n"; } But it won't work - the exception will be thrown anyway... Is there a way to catch a exception or disable it for a part of the code? -- Nico Link to comment Share on other sites More sharing options...
Wanze Posted March 10, 2013 Share Posted March 10, 2013 You can catch exceptions before Pw does, but I think the problem with your code is the include. Try writing the 'try' and 'catch' block inside your $file. In a project I have to import users with a cronjob and I'm doing something like this: try { $user->save(); } catch (Exception $e) { //.. write a Log with the Exception } 1 Link to comment Share on other sites More sharing options...
Nico Knoll Posted March 11, 2013 Author Share Posted March 11, 2013 Have to include the file because "eval" is crappy as shit and I have to execute php code. So I write the php code in a temp file and include this file... Link to comment Share on other sites More sharing options...
ryan Posted March 12, 2013 Share Posted March 12, 2013 Nico, I don't think that you can catch an include like that. You probably have to set a custom PHP error handler to capture it. It sounds like what you are getting is ProcessWire's error handler. Link to comment Share on other sites More sharing options...
Nico Knoll Posted March 12, 2013 Author Share Posted March 12, 2013 Yes it is a processwire error. But how to set a custom php handler? Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted September 20, 2016 Share Posted September 20, 2016 Good day! It seems like I got about the same question, so will post it here. I have this code $nextAction = $p->next_action; try { $this->$nextAction($p); } catch(Exception $e) { bd($e, 'Exeption!'); } and it does not work if a call a method that does not exist. I get a WireException, and it is not caught by try/catch. Is there a way to make it work? P.S. bd() is from awesome Tracy Debugger. If you do not use it yet you certainly have been away to a far away planet or do not do any code in PW . 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 20, 2016 Share Posted September 20, 2016 Are you using namespaces? If so then it would need to be (\Exception $e) 3 Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted September 20, 2016 Share Posted September 20, 2016 That is it, thanks. Link to comment Share on other sites More sharing options...
gebeer Posted November 7, 2017 Share Posted November 7, 2017 Hello, having the same problem as described by @Ivan Gretsky. Inside a namespaced module I have this method with a try catch block: public function addJson($pages, $filename) { $tmpPath = $this->files->tempDir('json'); try { $start = microtime(true); $jsondata = $this->getGeoJsonCollection($pages); $time = microtime(true) - $start; va_dump($time); // Note the intentional typo $filePath = $tmpPath . $filename; if($tmpPath && $jsondata) { //write json data into geojson.json file if(file_put_contents($filePath, $jsondata)) { $jsonPage = $this->jsonPage; $jsonPage->of(false); $file = $jsonPage->jsonfiles->getFile($filename); if($file) $jsonPage->jsonfiles->remove($file); $jsonPage->save(); $jsonPage->jsonfiles->add($filePath); $jsonPage->save(); if(!unlink($filePath)) $this->message("Error deleting temporary $filePath"); $this->message("$filename created and saved"); $this->log->save('json', "$filename created in $time seconds and saved"); return true; } else{ return false; } } } catch (\Exception $e) { $this->log->save('json', $e->getMessage()); } } I'm calling the module via command line from a worker php script that bootstraps PW. I get this ouput from my script and nothing logged to my custom json.text log file: PHP Fatal error: Call to undefined function ProcessWire\va_dump() in /var/www/uhtinew.dev/site/modules/SaveJson/SaveJson.module on line 208 Fatal error: Call to undefined function ProcessWire\va_dump() in /var/www/uhtinew.dev/site/modules/SaveJson/SaveJson.module on line 208 Error: Call to undefined function ProcessWire\va_dump() (line 208 of /var/www/uhtinew.dev/site/modules/SaveJson/SaveJson.module) This error message was shown because: site is in debug mode. ($config->debug = true; => /site/config.php). Error has been logged. So it seems that WireException is catching this before my try catch. Link to comment Share on other sites More sharing options...
Robin S Posted November 7, 2017 Share Posted November 7, 2017 9 hours ago, gebeer said: So it seems that WireException is catching this before my try catch. I don't think WireException is involved there. What you are seeing is a fatal error message from PHP, and you can't "catch" a fatal error AFAIK. 2 Link to comment Share on other sites More sharing options...
adrian Posted November 7, 2017 Share Posted November 7, 2017 Yeah, for fatal errors you need to look at: register_shutdown_function To be honest I was confused, because for me undefined functions are exceptions, not fatal errors. I just read that "php7 has changed this behavior, and undefined functions/methods are catchable exceptions." Some more on this change: "Many fatal and recoverable fatal errors have been converted to exceptions in PHP 7. These error exceptions inherit from the Error class, which itself implements the Throwable interface (the new base interface all exceptions inherit)." http://php.net/manual/en/migration70.incompatible.php 2 Link to comment Share on other sites More sharing options...
Alpine418 Posted March 28, 2023 Share Posted March 28, 2023 Any solution to catch a custom exception (e.g. RuntimeException) before ProcessWire does? I can catch it like in the first post above, but ProcessWire throws it anyway. Edit: Dont know why. But now it works. Strange. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now