javaunidatauniverseuniobjects

UniObjects for Java: How to get response String when error occurred in UniCommand.exec()


I would like to be able to determine the exact reason that a UniCommand could not complete using UniObjects for Java in order to tell the user. I have the following code that behaves as expected under ideal conditions, but if command is not a valid command, uniCommand.response() returns an empty String. I would like to know exactly why the command could not execute. I tried to useuniCommand.getSystemReturnCode(), but it always returns -1 if the command did not complete successfully and that's not enough information. How do I find out exactly what went wrong?

UniCommand uniCommand = uniSession.command();
uniCommand.setCommand(command);
uniCommand.exec();
int status = uniCommand.status();
//int sysRet = uniCommand.getSystemReturnCode();

if (status == UniObjectsTokens.UVS_COMPLETE) {
    output(uniCommand.response());
}

An Example: When I execute BLAH via telnet on the UniVerse server itself I get:

Verb "BLAH" is not in your VOC.

and when I execute LIST BLAH I get:

RetrieVe: syntax error.  Unexpected sentence without filename.  Token was "".
          Scanned command was LIST 'BLAH'

I would like to get those exact error messages in my program using UniObjects for Java. Is that possible?


Solution

  • I have had the same problem, and it does seem like a limitation of the uniobjects library. One way to handle it is to wrap the command in a subroutine.

    SUBROUTINE RUN.COMMAND(COMMAND,RESPONSE)
        EXECUTE COMMAND CAPTURING RESPONSE
    END
    

    Then use a UniSubroutine object to call it.

    String command = "LIST BLAH";
    UniSubroutine sub = uniSession.subroutine("RUN.COMMAND", 2);
    sub.setArg(0, command);
    sub.call();
    UniDynArray response = new UniDynArray(sub.getArg(1));
    
    for (int i = 0; i < response.dcount(); i++) {
        String line = response.extract(i).toString();
        System.out.println(line);
    }