Jump to content

Recommended Posts

Posted

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

Posted

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
}
  • Like 1
Posted

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...

Posted

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. 

  • 3 years later...
Posted

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 ;) .

  • Like 1
  • 1 year later...
Posted

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.

Posted
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.

  • Like 2
Posted

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

  • Like 2
  • 5 years later...
Posted

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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...