That won't catch everything. And it mixes data types for e. I use (and strongly recommend) this construct (shown as if a library script):
from java.lang import Throwable
logger = system.util.getLogger(system.util.getProjectName() + '.' + __name__)
def someTask():
try:
someaction()
except Throwable, t:
# Throwable is the ultimate superclass of all java exceptions and errors
logger.warn("some text for java", t)
except Exception, e
# Exception is python's superclass (technically BaseException--substitute if you like)
logger.warn("some text for jython", later.PythonAsJavaException(e))
Note that you cannot use jython exceptions with loggers, and you should always be using loggers (no prints). Which is why the separate except: clause is so important--you have extra work required to log a python exception. (PythonAsJavaException() is available in my later.py.)
If you have common handling needed for both exception cases, move that into another function.
Oh does the throwable catch basically all of the Java errors?
My exception cases for these are starting to look a bit ridiculous with all the different error types I'd been coming across, could never find a general exception as I very rarely want to do anything special based on the error.
try:
spamEggs()
except java.lang.Exception as e:
callBack(str(e))
except java.io.IOException as e:
callBack(str(e))
except Exception as e:
callBack(str(e))
def callBack(e)
logger.warn("Failed, {0}".format(e))
Yes. When you specify an exception type, that type and any subtypes are included. All java exceptions and errors inherit from Throwable, so you get them all there.
But it won't catch any jython errors--that's a different inheritance hierarchy.
You really should not be stringifying your exceptions--that drops the backtrace that you really need to debug complex problems. Pass them to the appropriate logger methods (look for the argument of type Throwable), which will include the backtrace in the logs.
response_header should be called something like length or bytes_read, and you need to use that to determine how many valid bytes were read into the buffer you provided.
Okay, how do I convert these bytes into chars and get them out of this array without them just becoming integers?
Something I found is if I take these numbers and convert them to binary, then convert those binary values into utf-8 encoding I start to piece together my JSON I want
In our message we get a 16 byte message header, this contains the length information for the rest of our JSON.
In this example I just have a hard cap on the buffer as 1024. And read that until its full, none of the messages are longer than that. Not a good solution, but workable while I test to see if we can even connect first. I'll come back around to patch that up later on, once I can kind of decode my JSON.
Ah, I think I've already got some check in there to unpack that header then. As it seems to send the data properly, I though it wasn't yet implemented.