use of com.aliyuncs.fc.exceptions.ClientException in project fc-java-sdk by aliyun.
the class PopClient method openFCService.
public OpenFunctionComputeResponse openFCService(Config config, OpenFunctionComputeRequest request) throws ClientException, ServerException {
request.validate();
try {
String accessKeyId = config.getAccessKeyID();
String accessSecret = config.getAccessKeySecret();
DefaultProfile profile = null;
if (StringUtils.isBlank(config.getSecurityToken())) {
profile = DefaultProfile.getProfile(OPEN_FC_SERVICE_REGION, accessKeyId, accessSecret);
} else {
String sToken = config.getSecurityToken();
profile = DefaultProfile.getProfile(OPEN_FC_SERVICE_REGION, accessKeyId, accessSecret, sToken);
}
OpenFcServiceRequest openFCServiceRequest = new OpenFcServiceRequest();
IAcsClient client = new DefaultAcsClient(profile);
openFCServiceRequest.setSysEndpoint(OPEN_FC_SERVICE_ENDPOINT);
OpenFcServiceResponse openFCServiceResponse = client.getAcsResponse(openFCServiceRequest);
OpenFunctionComputeResponse response = new OpenFunctionComputeResponse();
response.setRequestId(openFCServiceResponse.getRequestId());
response.setOrderId(openFCServiceResponse.getOrderId());
return response;
} catch (com.aliyuncs.exceptions.ClientException e) {
if (StringUtils.contains(e.getErrMsg(), "已开通")) {
OpenFunctionComputeResponse response = new OpenFunctionComputeResponse();
response.setRequestId(e.getRequestId());
response.setCode(ERROR_CODE_ORDER_OPENED);
response.setMsg("You have subscribed FunctionCompute, please proceed to FC console and start using it.");
return response;
}
throw new ClientException(e.getErrCode(), e.getErrMsg(), e.getRequestId());
}
}
use of com.aliyuncs.fc.exceptions.ClientException in project fc-java-sdk by aliyun.
the class AbstractResponseConsumer method getFcHttpResponse.
/**
* Convert apache HttpResponse to FC HttpResponse
* @return
*/
public com.aliyuncs.fc.http.HttpResponse getFcHttpResponse() throws Exception {
com.aliyuncs.fc.http.HttpResponse response = new com.aliyuncs.fc.http.HttpResponse();
// Status
response.setStatus(httpResponse.getStatusLine().getStatusCode());
// Headers
Header[] headers = httpResponse.getAllHeaders();
for (int i = 0; i < headers.length; i++) {
Header header = headers[i];
response.setHeader(header.getName(), header.getValue());
}
// Content
try {
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream in = entity.getContent();
response.setContent(IOUtils.toByteArray(in));
}
closeResponse(httpResponse);
} catch (IOException e) {
throw new ClientException("SDK.ServerUnreachable", "Server unreachable: " + e.toString());
}
if (response.getStatus() >= 500) {
String requestId = response.getHeader(HeaderKeys.REQUEST_ID);
String stringContent = response.getContent() == null ? "" : FcUtil.toDefaultCharset(response.getContent());
ServerException se;
try {
se = new Gson().fromJson(stringContent, ServerException.class);
} catch (JsonParseException e) {
se = new ServerException("InternalServiceError", "Failed to parse response content", requestId);
}
se.setStatusCode(response.getStatus());
se.setRequestId(requestId);
throw se;
} else if (response.getStatus() >= 300) {
ClientException ce;
if (response.getContent() == null) {
ce = new ClientException("SDK.ServerUnreachable", "Failed to get response content from server");
} else {
try {
ce = new Gson().fromJson(FcUtil.toDefaultCharset(response.getContent()), ClientException.class);
} catch (JsonParseException e) {
ce = new ClientException("SDK.ResponseNotParsable", "Failed to parse response content", e);
}
}
if (ce == null) {
ce = new ClientException("SDK.UnknownError", "Unknown client error");
}
ce.setStatusCode(response.getStatus());
ce.setRequestId(response.getHeader(HeaderKeys.REQUEST_ID));
throw ce;
}
return response;
}
use of com.aliyuncs.fc.exceptions.ClientException in project fc-java-sdk by aliyun.
the class FunctionComputeClientTest method testUpdateFunctionValidate.
@Test
public void testUpdateFunctionValidate() {
try {
UpdateFunctionRequest request = new UpdateFunctionRequest(SERVICE_NAME, null);
client.updateFunction(request);
fail("ClientException is expected");
} catch (ClientException e) {
assertTrue(e.getMessage().contains(VALIDATE_MSG));
}
try {
UpdateFunctionRequest request = new UpdateFunctionRequest(SERVICE_NAME, "");
client.updateFunction(request);
fail("ClientException is expected");
} catch (ClientException e) {
assertTrue(e.getMessage().contains(VALIDATE_MSG));
}
try {
UpdateFunctionRequest request = new UpdateFunctionRequest(null, FUNCTION_NAME);
client.updateFunction(request);
fail("ClientException is expected");
} catch (ClientException e) {
assertTrue(e.getMessage().contains(VALIDATE_MSG));
}
try {
UpdateFunctionRequest request = new UpdateFunctionRequest("", FUNCTION_NAME);
client.updateFunction(request);
fail("ClientException is expected");
} catch (ClientException e) {
assertTrue(e.getMessage().contains(VALIDATE_MSG));
}
}
use of com.aliyuncs.fc.exceptions.ClientException in project fc-java-sdk by aliyun.
the class FunctionComputeClientTest method testListServices.
@Test
public void testListServices() {
final int numServices = 10;
final int limit = 3;
// Create multiple services
for (int i = 0; i < numServices; i++) {
try {
client.getService(new GetServiceRequest(SERVICE_NAME + i));
cleanupService(SERVICE_NAME + i);
} catch (ClientException e) {
if (!ErrorCodes.SERVICE_NOT_FOUND.equals(e.getErrorCode())) {
throw new RuntimeException("Cleanup failed");
}
}
CreateServiceRequest request = new CreateServiceRequest();
request.setServiceName(SERVICE_NAME + i);
request.setDescription(SERVICE_DESC_OLD);
request.setRole(ROLE);
CreateServiceResponse response = client.createService(request);
assertFalse(Strings.isNullOrEmpty(response.getRequestId()));
}
ListServicesRequest listRequest = new ListServicesRequest();
listRequest.setLimit(limit);
listRequest.setPrefix(SERVICE_NAME);
ListServicesResponse listResponse = client.listServices(listRequest);
int numCalled = 1;
String nextToken = listResponse.getNextToken();
while (nextToken != null) {
listRequest.setNextToken(nextToken);
listResponse = client.listServices(listRequest);
nextToken = listResponse.getNextToken();
numCalled++;
}
assertEquals(numServices / limit + 1, numCalled);
// Delete services
for (int i = 0; i < numServices; i++) {
cleanupService(SERVICE_NAME + i);
}
}
use of com.aliyuncs.fc.exceptions.ClientException in project fc-java-sdk by aliyun.
the class FunctionComputeClientTest method testUpdateTriggerValidate.
@Test
public void testUpdateTriggerValidate() {
try {
UpdateTriggerRequest request = new UpdateTriggerRequest(SERVICE_NAME, FUNCTION_NAME, null);
client.updateTrigger(request);
fail("ClientException is expected");
} catch (ClientException e) {
assertTrue(e.getMessage().contains(VALIDATE_MSG));
}
try {
UpdateTriggerRequest request = new UpdateTriggerRequest(SERVICE_NAME, FUNCTION_NAME, "");
client.updateTrigger(request);
fail("ClientException is expected");
} catch (ClientException e) {
assertTrue(e.getMessage().contains(VALIDATE_MSG));
}
try {
UpdateTriggerRequest request = new UpdateTriggerRequest(SERVICE_NAME, null, TRIGGER_NAME);
client.updateTrigger(request);
fail("ClientException is expected");
} catch (ClientException e) {
assertTrue(e.getMessage().contains(VALIDATE_MSG));
}
try {
UpdateTriggerRequest request = new UpdateTriggerRequest(SERVICE_NAME, "", TRIGGER_NAME);
client.updateTrigger(request);
fail("ClientException is expected");
} catch (ClientException e) {
assertTrue(e.getMessage().contains(VALIDATE_MSG));
}
try {
UpdateTriggerRequest request = new UpdateTriggerRequest(null, FUNCTION_NAME, TRIGGER_NAME);
client.updateTrigger(request);
fail("ClientException is expected");
} catch (ClientException e) {
assertTrue(e.getMessage().contains(VALIDATE_MSG));
}
try {
UpdateTriggerRequest request = new UpdateTriggerRequest("", FUNCTION_NAME, TRIGGER_NAME);
client.updateTrigger(request);
fail("ClientException is expected");
} catch (ClientException e) {
assertTrue(e.getMessage().contains(VALIDATE_MSG));
}
}
Aggregations