# Project Library Script Error Message

**URL:** <https://forum.inductiveautomation.com/t/project-library-script-error-message/52746>\
**Category:** Ignition\
**Created:** [November 4, 2021, 7:25pm UTC](https://forum.inductiveautomation.com/t/project-library-script-error-message/52746 "2021-11-04T19:25:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mg12092](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@mg12092](https://forum.inductiveautomation.com/u/mg12092)\
**Post date:** [November 4, 2021, 7:25pm UTC](https://forum.inductiveautomation.com/t/project-library-script-error-message/52746/1 "2021-11-04T19:25:10Z")

</div>

In the project library I created save script:

```auto
def saveData(data):
   try:
       ::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.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [November 4, 2021, 7:36pm UTC](https://forum.inductiveautomation.com/t/project-library-script-error-message/52746/2 "2021-11-04T19:36:40Z")

</div>

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

```auto
#gather data
try:
    myModule.saveData(data)
except FormError as e:
    displayError(e)

```

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

```auto
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

```auto
try:
    myModule.saveData(data)
except FormError as e:
    system.gui.messageBox(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

```auto
class FormError(Exception):
    pass

```

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.

---

<div class="post-metadata">

**Author:** ![mg12092](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@mg12092](https://forum.inductiveautomation.com/u/mg12092)\
**Post date:** [November 4, 2021, 7:57pm UTC](https://forum.inductiveautomation.com/t/project-library-script-error-message/52746/3 "2021-11-04T19:57:26Z")

</div>

Thanks,

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

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

Thanks!

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [November 4, 2021, 7:59pm UTC](https://forum.inductiveautomation.com/t/project-library-script-error-message/52746/4 "2021-11-04T19:59:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 4, 2021, 8:11pm UTC](https://forum.inductiveautomation.com/t/project-library-script-error-message/52746/5 "2021-11-04T20:11:53Z")

</div>

> [@bkarabinchak.psi](#):
>
> BaseException

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

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [November 5, 2021, 12:44pm UTC](https://forum.inductiveautomation.com/t/project-library-script-error-message/52746/6 "2021-11-05T12:44:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 5, 2021, 1:29pm UTC](https://forum.inductiveautomation.com/t/project-library-script-error-message/52746/7 "2021-11-05T13:29:53Z")

</div>

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