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

心不动则不痛 提交于 2019-12-08 07:38:54

问题


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?


回答1:


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);
}


来源:https://stackoverflow.com/questions/19146112/uniobjects-for-java-how-to-get-response-string-when-error-occurred-in-unicomman

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!