package com.perforce.p4java_ext; import com.perforce.p4java.impl.mapbased.rpc.msg.RpcMessage; import java.util.Map; /** * Maps output of the Map command to our P4Exception class when error conditions * are reported. */ public interface P4Errors { public static final int E_EMPTY = 0; public static final int E_INFO = 1; public static final int E_WARN = 2; public static final int E_FAILED = 3; public static final int E_FATAL = 4; default void checkError(Map<String, Object>[] maps) { checkError(maps, null); } default void checkError(Map<String, Object>[] maps, ErrorId[] errors) { if (maps == null) return; // check error and throw exception for (Map<String, Object> map : maps) { for (int i = 0; ; i++) { String code = "code" + i; if (map.containsKey(code)) { String codeValue = (String) map.get(code); String suffix = " serverCode=[" + codeValue + "]"; String message = getServerMessage(map, E_INFO); boolean isAuthFail = isAuthFail(message); if (isAuthFail) { throw new P4Exception(message + suffix); } else if (RpcMessage.getSeverity(codeValue) == E_WARN) { throw new P4Exception(message + suffix, codeValue); } else if (RpcMessage.getSeverity(codeValue) >= E_WARN) { throw new P4Exception(message + suffix, codeValue); } else if (isNotSeriousErrorButNeedReport(codeValue)) { // if this is not an error but a warning, how does // client code need to deal with it? throw new P4Exception(message + suffix, codeValue); } // loop through other errors too if (errors != null) for (int eid = 0; eid < errors.length; eid++) { if (errors[eid].match(codeValue)) throw new P4Exception( message + suffix, codeValue); } } else break; } } } public static boolean isNotSeriousErrorButNeedReport(String code) { if (ErrorIds.MsgDm_LockAlreadyOther.match(code)) return true; return false; } public static String getServerMessage(Map<String, Object> map, int minimumCode) { if (map == null) return null; int index = 0; String code = (String) map.get(RpcMessage.CODE + index); // Return if no code0 key found if (code == null) return null; boolean foundCode = false; StringBuilder codeString = new StringBuilder(); while (code != null) { int severity = RpcMessage.getSeverity(code); if (severity >= minimumCode) { foundCode = true; String fmtStr = (String) map.get(RpcMessage.FMT + index); if (fmtStr != null) { if (fmtStr.indexOf('%') != -1) { fmtStr = RpcMessage.interpolateArgs(fmtStr, map); } // Insert latest message at beginning of error string // since server structures them this way codeString.insert(0, fmtStr); codeString.insert(fmtStr.length(), '\n'); } } index++; code = (String) map.get(RpcMessage.CODE + index); } // Only return a string if at least one severity code was found if (foundCode) { return codeString.toString(); } return null; } public static boolean isAuthFail(String message) { if (message == null) return false; for (String str : ACCESS_ERROR_MESSAGES) { if (message.contains(str)) return true; } return false; } /** * Signals access (login) needed */ static final String CORE_AUTH_FAIL_STRING_1 = "Perforce password (P4PASSWD)"; /** * Signals access (login) needed */ static final String CORE_AUTH_FAIL_STRING_2 = "Access for user"; /** * Signals ticket has expired */ static final String CORE_AUTH_FAIL_STRING_3 = "Your session has expired"; /** * Signals ticket has expired */ static final String CORE_AUTH_FAIL_STRING_4 = "Your session was logged out"; /** * SSO Failure. */ static final String CORE_AUTH_FAIL_STRING_5 = "Single sign-on on client failed"; static final String[] ACCESS_ERROR_MESSAGES = { CORE_AUTH_FAIL_STRING_1, CORE_AUTH_FAIL_STRING_2, CORE_AUTH_FAIL_STRING_3, CORE_AUTH_FAIL_STRING_4, CORE_AUTH_FAIL_STRING_5}; }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 9086 | tjuricek |
0.1.0 Initial release of p4java_ext Mostly just contains enough of the P4 API to build the test data initializer. About 4 commands have minimal testing, so it works. Kind of. |
||
#1 | 9085 | tjuricek | First import of the project, before package refactoring. |