private Exception refineException(HttpResponseException exception) { Exception refinedException = exception; String error = exception.getContent(); // Create custom exception for error messages we know about if (SYSTEM_RECONFIGURING_PATTERN.matcher(error).matches()) { refinedException = new IllegalStateException(exception); } else if (RESOURCE_NOT_FOUND_PATTERN.matcher(error).matches()) { refinedException = new ResourceNotFoundException(error, exception); } else if (ILLEGAL_STATE_PATTERN.matcher(error).matches()) { refinedException = new IllegalStateException(error, exception); } else if (ILLEGAL_ARGUMENT_PATTERN.matcher(error).matches()) { refinedException = new IllegalArgumentException(error, exception); } else if (AUTH_PATTERN.matcher(error).matches()) { refinedException = new AuthorizationException(error, exception); } else if (NOT_IMPLEMENTED_PATTERN.matcher(error).matches()) { refinedException = new UnsupportedOperationException(error, exception); } return refinedException; }
private Exception refineException(HttpResponseException exception) { Exception refinedException = exception; String error = exception.getContent(); // Create custom exception for error messages we know about if (SYSTEM_RECONFIGURING_PATTERN.matcher(error).matches()) { refinedException = new IllegalStateException(exception); } else if (RESOURCE_NOT_FOUND_PATTERN.matcher(error).matches()) { refinedException = new ResourceNotFoundException(error, exception); } else if (ILLEGAL_STATE_PATTERN.matcher(error).matches()) { refinedException = new IllegalStateException(error, exception); } else if (ILLEGAL_ARGUMENT_PATTERN.matcher(error).matches()) { refinedException = new IllegalArgumentException(error, exception); } else if (AUTH_PATTERN.matcher(error).matches()) { refinedException = new AuthorizationException(error, exception); } else if (NOT_IMPLEMENTED_PATTERN.matcher(error).matches()) { refinedException = new UnsupportedOperationException(error, exception); } return refinedException; }
private KeyPair createNewKeyPairInOrg(URI org, String keyPairName) { checkNotNull(org, "org"); checkNotNull(keyPairName, "keyPairName"); logger.debug(">> creating keyPair org(%s) name(%s)", org, keyPairName); KeyPair keyPair = null; while (keyPair == null) { try { keyPair = trmkClient.generateKeyPairInOrg(org, getNextName(keyPairName), false); logger.debug("<< created keyPair(%s)", keyPair.getName()); } catch (RuntimeException e) { HttpResponseException ht = Throwables2.getFirstThrowableOfType(e, HttpResponseException.class); if (ht == null || ht.getContent() == null || ht.getContent().indexOf("Security key with same name exists") == -1) throw e; } } return keyPair; }
@Override public Object apply(Exception e) { if (e instanceof HttpResponseException) { HttpResponseException responseException = (HttpResponseException) e; if (responseException.getContent() != null) { Type setType = new TypeToken<Map<String, RimuHostingResponse>>() { }.getType(); String test = responseException.getContent(); Map<String, RimuHostingResponse> responseMap = json.fromJson(test, setType); RimuHostingResponse firstResponse = Iterables.get(responseMap.values(), 0); String errorClass = firstResponse.getErrorInfo().getErrorClass(); if (errorClass.equals("PermissionException")) throw new AuthorizationException( firstResponse.getErrorInfo().getErrorMessage(), responseException); throw new RuntimeException(firstResponse.getErrorInfo().getErrorMessage(), e); } } throw Throwables.propagate(e); } }