生成自定义状态以及错误消息

时间:2015-06-30 19:58:21

标签: java status

如何生成自定义状态和errorDetails消息。我正在从事网络服务。最初我是随机生成的。下面是代码。但现在,我想删除它的随机功能。我的整个目标是使用错误消息设置例外(例如)并将其发送回客户端。

String[] status = { "SUCCESS", "ERROR", "LOCKED", "EXCEPTION" };
Random ran = new Random();
String gen = status[ran.nextInt(status.length)];
logger.debug("Status is" + status);
outData.setStatus(gen);

if (gen.equals("SUCCESS")) {
    outData.setErrorDetails("");
} else if (gen.equals("ERROR")) {
    outData.setErrorDetails("Usage of time code VAC is not allowed; balance is insufficient." + " error");
} else if (gen.equals("LOCKED")) {
    outData.setErrorDetails("Timesheet cannot be edited because it is locked for payroll close." + "locked");
} else if (gen.equals("EXCEPTION")) {
    outData.setErrorDetails("{ML}QR_INCORRECT_CONDITION_PARAMETER{/ML}Error in condition AWA Is Self Override Condition:java.lang.NullPointerException{ARGS}AWA Is Self Override Conditionjava.lang.NullPointerException{/ARGS" + "exception");
} 
returnList.add(outData);

1 个答案:

答案 0 :(得分:0)

使用HashMap。这会减少你的代码。

使用像 -

这样的东西
Map<String, String> statusMsgMap = new HashMap<String, String>();
statusMsgMap.put("SUCCESS", "Your Success Message");
statusMsgMap.put("ERROR", "Your Error Message");
statusMsgMap.put("LOCKED", "Your Locked Message");
statusMsgMap.put("EXCEPTION", "Your Exception Message");

outData.setErrorDetails(statusMsgMap.get(gen));