Project Library Script Error Message

In the project library I created save script:

def saveData(data):
       ::do something::
   except BaseException as err:
       errorMsg = str(err)

   return errorMsg

That’s the pseudo code for it, but when the script fails (intentional for testing) I get the error message in the log but it doesn’t get returned to my perspective screen.

Instead it seems like my perspective event script just stops at that line and doesn’t continue to execute.

What’s the right way to catch an error in the project library and pass it back to my screen where it’s called without breaking the script execution.

i’ve also tried to call the project library by encapsulating it within a try / except, but that doesn’t work either.

So here’s an example of what I do on the front end part of a Vision (though could be perspective app as well) - the following would be on a submit button

#gather data
except FormError as e:

And then inside my saveData(data), I have conditions that are checked. For instance

def saveData(data):
    data: dictionary of data
    if data['someMoneyField'] < 0:
        raise FormError("Money field cannot be negative!")

So that first tidbit of code would catch the error that occurs when actually trying to save it, and then you can do with it as you wish to display it. In vision, a very easy way to do it would be

except FormError as e:

to show a popup of the error. In perspective, you’ll probably want to make your own popup.

Note - FormError, is a custom error I made, for situations where the user enters input that is considered not valid. You can and in some cases should make your own custom error types when appropriate. To do so

class FormError(Exception):

is all you need, and then import this where needed.

Also, normally you don’t return the errorMsg like that - what if your try is completely successful, then your errorMsg would be None and that is what would be returning. Usually in situations where you are unsure of the errors you might get, you should at the least just log them to the gateway, so that your try/excepts can be better organized so that for errors you do now know occur, you can handle appropriately.

One of the general rules for coding python is that the “right” or clean way to code is to try to do something and then fail gracefully. However, when it comes to failing gracefully from other code that is up to you to write, the only way to actually even fail is if your other code does a raise ErrorOfSomeSort somewhere.

I hope this makes sense.


The trick was that the project library doesn’t like:

except BaseException as err:

Instead it only wants an except:

But if my function calls another function, and i want to roll-up the error messages, the exception class you had was need to be able to catch it. That was perfect, it’s just what i needed.


Hmm that’s odd. I almost never use except by itself. IIRC, perhaps BaseException needs to be specifically imported? Not sure.

But as long as the rest makes sense.

This won’t catch pure java exceptions. I usually import Throwable and catch that in a separate except clause.

1 Like

Yea thats a good idea especially if you’re not sure what sort of java exception could come from the system.* function you are calling.

Now that I think about it, it would be really nice of the documentation said what sort of error it could throw. Or do they all come through as Throwable with just varying error messages?

Throwable is the underlying class for all java exceptions. Actual exceptions will be subclasses, and can be examined.

1 Like