Search in sources :

Example 11 with ClientException

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());
    }
}
Also used : OpenFunctionComputeResponse(com.aliyuncs.fc.response.OpenFunctionComputeResponse) DefaultProfile(com.aliyuncs.profile.DefaultProfile) DefaultAcsClient(com.aliyuncs.DefaultAcsClient) OpenFcServiceResponse(com.aliyuncs.fc_open.model.v20200310.OpenFcServiceResponse) OpenFcServiceRequest(com.aliyuncs.fc_open.model.v20200310.OpenFcServiceRequest) IAcsClient(com.aliyuncs.IAcsClient) ClientException(com.aliyuncs.fc.exceptions.ClientException)

Example 12 with ClientException

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;
}
Also used : ServerException(com.aliyuncs.fc.exceptions.ServerException) HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) Gson(com.google.gson.Gson) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) Header(org.apache.http.Header) ClientException(com.aliyuncs.fc.exceptions.ClientException)

Example 13 with ClientException

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));
    }
}
Also used : ClientException(com.aliyuncs.fc.exceptions.ClientException) UpdateFunctionRequest(com.aliyuncs.fc.request.UpdateFunctionRequest) Test(org.junit.Test)

Example 14 with ClientException

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);
    }
}
Also used : ListServicesRequest(com.aliyuncs.fc.request.ListServicesRequest) CreateServiceRequest(com.aliyuncs.fc.request.CreateServiceRequest) ListServicesResponse(com.aliyuncs.fc.response.ListServicesResponse) CreateServiceResponse(com.aliyuncs.fc.response.CreateServiceResponse) GetServiceRequest(com.aliyuncs.fc.request.GetServiceRequest) ClientException(com.aliyuncs.fc.exceptions.ClientException) Test(org.junit.Test)

Example 15 with ClientException

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));
    }
}
Also used : UpdateTriggerRequest(com.aliyuncs.fc.request.UpdateTriggerRequest) ClientException(com.aliyuncs.fc.exceptions.ClientException) Test(org.junit.Test)

Aggregations

ClientException (com.aliyuncs.fc.exceptions.ClientException)26 Test (org.junit.Test)13 IOException (java.io.IOException)6 Gson (com.google.gson.Gson)5 ServerException (com.aliyuncs.fc.exceptions.ServerException)4 FunctionComputeClient (com.aliyuncs.fc.client.FunctionComputeClient)3 PrepareUrl (com.aliyuncs.fc.model.PrepareUrl)3 JsonParseException (com.google.gson.JsonParseException)3 SocketTimeoutException (java.net.SocketTimeoutException)3 InvalidKeyException (java.security.InvalidKeyException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 HttpResponse (com.aliyuncs.fc.http.HttpResponse)2 CreateServiceRequest (com.aliyuncs.fc.request.CreateServiceRequest)2 GetServiceRequest (com.aliyuncs.fc.request.GetServiceRequest)2 URISyntaxException (java.net.URISyntaxException)2 DefaultAcsClient (com.aliyuncs.DefaultAcsClient)1 IAcsClient (com.aliyuncs.IAcsClient)1 BasicSessionCredentials (com.aliyuncs.auth.BasicSessionCredentials)1 SignURLConfig (com.aliyuncs.fc.auth.SignURLConfig)1 Config (com.aliyuncs.fc.config.Config)1