Maximo Open Forum

 View Only
  • 1.  Unable to catch exceptions in automation script

    Posted 07-21-2025 10:36

    I have an automation script that I am working on that implements a REST API request. I'm having a problem trapping exceptions coming from java method calls. In this case, I am calling the add() method on an mbo opened on the PLUSDESTVERSION table. There is a custom java validation that throws an exception if the value assigned to the WORKGROUP field isn't a valid record. I have added multiple levels of "try/except" blocks around the add() call, but the except blocks aren't getting invoked, and my REST API call is returning a 400 status, with the response JSON body set to the data from the java error.

    Here is the code:

                try:
                    version = versionSet.add()
                    version.setValue("CONTRIBUTIONMETHOD", "N")
                    version.setValue("APPLYCONTRIBUTION", "N")
                    log("Before setValue(WORKGROUP)")
                    try:
                        version.setValue("WORKGROUP", parentWO.getString("OWNERGROUP"))
                    except Exception as addEx:
                        log_error("EXCEPTION during version.setValue(): {}".format(str(addEx)))
                        raise Exception("EXCEPTION during version.setValue(): {}".format(str(addEx)))
                    
                    log("After setValue(WORKGROUP)")
                    version.setValue("OVERHEADTYPE", parentWO.getString("OPSSITEID"))
                    if ogsLvInitiatedCue == "Y":
                        version.setValue("STORELOC", storeLoc)
                        if opsSiteId == "ONG":
                            # Set default for ONG only
                            log("Setting PLUSDESTVERSION.OGSREIMBURSABLE for ONG")
                            version.setValue("OGSREIMBURSABLE", "No")
                        
                    versionSet.save()
                except Exception as addEx:
                    log_error("EXCEPTION during versionSet.add(): {}".format(str(addEx)))
                    raise Exception("EXCEPTION during versionSet.add(): {}".format(str(addEx)))

    Rather than my exception message appearing in the REST call's response body, I'm getting this:

    {
        "Error": {
            "errorattrname": "workgroup",
            "extendedError": {
                "moreInfo": {
                    "href": "http://localhost/maximo/api/error/messages/BMXAP0395E"
                }
            },
            "correlationid": null,
            "errattrvalue": null,
            "reasonCode": "BMXAP0395E",
            "message": "BMXAP0395E - Person Group OTRE is not a valid TnD Work Group.",
            "statusCode": "400"
        }
    }
    My log messages are also not getting written to SystemOut.log.

    Is what I am doing correct? Or are automation scripts unable to trap exceptions from calls to IBM java classes?

    #Customizations
    #Integrations

    ------------------------------
    Theo Pozzy
    OneGas
    ------------------------------


  • 2.  RE: Unable to catch exceptions in automation script

    Posted 07-21-2025 21:00

    It looks like you're doing processing as an OSIN automation script based on the format of the error being returned. While not the question, the most optimal place to generate an error is in the beforeProcess. If you're able to tell based on the payload whether the record will be invalid, it's better to find it and generate an error there before Maximo tries to process the MBOs at all.

    When you say except Exception as addEx:, that works for Jython exceptions, not Maximo exceptions. If you don't really care about the exception type, catch it generically by doing this:

    import sys

    except:
        errorMessage = "Error: " + str(sys.exc_info()[0]) + ". Message: " + str(sys.exc_info()[1])


    I would avoid raising exceptions using the raise Exception syntax. Throw an error using the Maximo framework service.error (or ctx.error in this case) instead. Otherwise, you'll get an ugly looking error message because Maximo can't parse the exception. Using the Maximo framework also enables you to re-use error messages, translate, etc. 


    I'm a bit confused by the logic because you try except and raise the exception and have a try except that raise an exception. I assume you already have a useful error message when the WORKGROUP field is set so I can't imagine you'd get anything more valuable by intercepting it earlier.

    You also should almost never be calling a save() in an OSIN automation script. Your sets should be child sets of the main object (IE WORKORDER in this scenario) and committed or rolled back together automatically by the Maximo framework. The only reason you would need to call save() is if you open the set using MXServer (MXServer.getMXServer().getMboSet("OBJECTNAME",userInfo)) which would establish a new transaction. This can lead to data getting committed even if the rest of the transaction gets rolled back. 



    ------------------------------
    Steven Shull
    Naviam
    ------------------------------



  • 3.  RE: Unable to catch exceptions in automation script

    Posted 07-22-2025 10:24

    Stephen,

    This isn't an OSIN script. It is part of a big stand-alone script that creates T&D CU estimates, versions, stations, work orders, etc. The script creates multiple objects and saves each of them. I'm trying to trap all errors so I can return a consistent error message in the JSON response, regardless of whether it is an exception from the IBM java code, or an error condition that I detect in the script logic.

    I will look into using the service.error call. 



    ------------------------------
    Theo Pozzy
    OneGas
    ------------------------------