I get an exception infrequently when performing a write to a database table from a script called by a Gateway timer.

I have the write wrapped by try…except, and I write to a backup table if the first fails. In order to find the cause for the exception I need access to the GatewayException and\or SQLServerException from the traceback object. All I can seem to get is the Exception, which just tells me there was an error executing that query, not the cause.

I am testing using the following code that is ran by a button press. ProductionDemanded does not exist.

import traceback import system.db try: a = system.db.runQuery("SELECT * FROM ProductionDemanded") except: system.gui.messageBox("%s" % traceback.format_exc())

How can I do this?

If you didn’t catch it, it would probably get logged. But I suppose you don’t want to kill your script over it. You could do this to log it to Log4J, which will include the entire stack and message:

from org.apache.log4j import LogManager
import java.lang.Exception

	a = system.db.runQuery("SELECT * FROM ProductionDemanded")
except java.lang.Exception, err:
	LogManager.getLogger("MyScript").error("Error running query.", err)

Small side node: if you enclose code in “[ code ] [/ code ]” it looks better.


No, I don’t want to kill the script, that is the purpose for the backup table. I will give the logging a shot. Thanks.

Where is this log written?

From a gateway script, this would show up in the gateway console, under System>Console in the gateway config. It also gets written to “wrapper.log” under “logs” in the install directory.


The above solution has been working great for SQL errors. Now I’d like to capture other errors. I was trying to capture the NameError for a test. I confirmed I am getting into the “except”, but then I get an TypeError returned by the getLogger statement. Is “MyScript” the problem?

from org.apache.log4j import LogManager
import java.lang.Exception
import exceptions
except java.lang.Exception, err:
     LogManager.getLogger("MyScript").error("Script Query Error.", err)
except NameError, err:
     LogManager.getLogger("MyScript").error("Name Error.", err)
     #Something else

Hard to say, what’s the details of your error now?