Skip to content

Commit

Permalink
improve the error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Jul 22, 2024
1 parent 8742514 commit 35caf7c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ private void checkProductCode(ResolveEndpointRequest request) throws ClientExcep
}

if (!productCodeValid) {
String fmt = "No endpoint for product '%s'.\n" +
"Please check the product code, or set an endpoint for your request explicitly.\n" +
"See https://api.alibabacloud.com/product/%s\n";

throw new ClientException(ErrorCodeConstant.SDK_ENDPOINT_RESOLVING_ERROR,
String.format(ErrorMessageConstant.ENDPOINT_NO_PRODUCT, request.productCode));
String.format(fmt, request.productCode, request.productCode));
}
}

Expand Down Expand Up @@ -70,9 +74,16 @@ public String resolve(ResolveEndpointRequest request) throws ClientException {

checkProductCode(request);
checkRegionId(request);

throw new ClientException(ErrorCodeConstant.SDK_ENDPOINT_RESOLVING_ERROR,
String.format(ErrorMessageConstant.ENDPOINT_NO_REGION, request.regionId, request.productCode,
getAvailableRegionsHint(request.productCode)));
String fmt = "No endpoint in the region '%s' for product '%s'.\n" +
"You can set an endpoint for your request explicitly.%s\n" +
"See https://api.alibabacloud.com/product/%s";
String message = String.format(
fmt,
request.regionId,
request.productCode,
getAvailableRegionsHint(request.productCode),
request.productCode
);
throw new ClientException(ErrorCodeConstant.SDK_ENDPOINT_RESOLVING_ERROR, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,8 @@
package com.aliyuncs.exceptions;

public class ErrorMessageConstant {

public static final String SDK_ENDPOINT_MANAGEMENT_DOC_HTML =
"https://www.alibabacloud.com/help/zh/doc-detail/92049.htm";

public static final String INVALID_REGION_ID =
"No such region '%s'. Please check your region ID.";
public static final String ENDPOINT_NO_REGION =
"No endpoint in the region '%s' for product '%s'. \n" +
"You can set an endpoint for your request explicitly.%s\n" +
"See " + SDK_ENDPOINT_MANAGEMENT_DOC_HTML + "\n";

/**
* Or use available regions:
*/
public static final String ENDPOINT_NO_PRODUCT =
"No endpoint for product '%s'. \n" +
"Please check the product code, or set an endpoint for your request explicitly.\n" +
"See " + SDK_ENDPOINT_MANAGEMENT_DOC_HTML + "\n";

public static final String SERVER_RESPONSE_HTTP_BODY_EMPTY =
"Failed to parse the response. The request was succeeded, but the server returned an empty HTTP body.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import org.junit.Assert;
import org.junit.Test;

import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import org.junit.function.ThrowingRunnable;

public class DefaultEndpointResolverTest {
@Test
Expand Down Expand Up @@ -50,4 +53,25 @@ public void testConstructor() throws ClientException {
assertEquals("cloudapi-vpc.test.aliyuncs.com", resolver.resolve(request));
}

@Test
public void testConstructorWithCustomLocationService() throws ClientException {
DefaultAcsClient client = mock(DefaultAcsClient.class);
DefaultProfile profile = DefaultProfile.getProfile();
profile.setLocationServiceEndpoint("location-readonly.aliyuncs.com");
profile.setLocationServiceApiVersion("2015-12-25");
final DefaultEndpointResolver resolver = new DefaultEndpointResolver(
client, "{}", profile);

final ResolveEndpointRequest request = new ResolveEndpointRequest("test", "cloudapi", null, null);
ClientException ex = Assert.assertThrows(ClientException.class, new ThrowingRunnable() {
@Override
public void run() throws Throwable {
resolver.resolve(request);
}
});
Assert.assertEquals("SDK.EndpointResolvingError : No endpoint in the region 'test' for product 'cloudapi'.\n" +
"You can set an endpoint for your request explicitly.\n" +
"Or you can use the other available regions: cn-shenzhen cn-beijing ap-south-1 eu-west-1 ap-northeast-1 me-east-1 cn-qingdao cn-shanghai cn-hongkong ap-southeast-1 ap-southeast-2 ap-southeast-3 eu-central-1 cn-huhehaote ap-southeast-5 us-east-1 us-west-1 cn-zhangjiakou cn-hangzhou\n" +
"See https://api.alibabacloud.com/product/cloudapi", ex.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class ClientExceptionTest {

@Test
public void constructorTest0() {
Map<String, Object> detail = new HashMap<String, Object>();
ClientException ex = new ClientException("code", "message", "requestid", "description", detail);
Assert.assertSame(detail, ex.getAccessDeniedDetail());
ClientException ex4 = new ClientException("code", "message", new Exception("cause"));
Assert.assertEquals(ErrorType.Client, ex4.getErrorType());
}

@Test
public void constructorTest() {
ClientException ex = new ClientException("client exception message");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ public void testCreateSSLSocketFactoryWithManagers() throws Exception {
}

@Test
@Ignore
public void testThrowSSLHandshakeException() throws ClientException, IOException {
thrown.expect(SSLHandshakeException.class);
HttpClientConfig clientConfig = HttpClientConfig.getDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void testLocationServiceMiss() throws ClientException {
Assert.assertEquals(ErrorCodeConstant.SDK_ENDPOINT_RESOLVING_ERROR, e.getErrCode());
Assert.assertEquals("No endpoint in the region 'mars' for product 'Ram'. \n" +
"You can set an endpoint for your request explicitly.\n" +
"See https://www.alibabacloud.com/help/zh/doc-detail/92049.htm\n", e.getErrMsg());
"See https://api.alibabacloud.com/product/Ram", e.getErrMsg());
}
}
// Bad region ID with another product
Expand All @@ -209,7 +209,7 @@ public void testLocationServiceMiss() throws ClientException {
Assert.assertEquals("No endpoint in the region 'mars' for product 'Ecs'. \n" +
"You can set an endpoint for your request explicitly.\n" +
"Or you can use the other available regions: cn-shenzhen cn-beijing ap-south-1 eu-west-1 ap-northeast-1 me-east-1 cn-qingdao cn-shanghai cn-hongkong ap-southeast-1 ap-southeast-2 ap-southeast-3 eu-central-1 cn-huhehaote ap-southeast-5 us-east-1 cn-zhangjiakou us-west-1 cn-hangzhou\n" +
"See https://www.alibabacloud.com/help/zh/doc-detail/92049.htm\n", e.getErrMsg());
"See https://api.alibabacloud.com/product/Ecs", e.getErrMsg());
}
Assert.assertEquals(3, locationServiceEndpointResolver.locationServiceCallCounter);

Expand Down Expand Up @@ -244,10 +244,10 @@ public void testTryToGetEndpointWithInvalidRegionId() {
Assert.fail();
} catch (ClientException e) {
Assert.assertEquals(ErrorCodeConstant.SDK_ENDPOINT_RESOLVING_ERROR, e.getErrCode());
Assert.assertEquals("No endpoint in the region 'mars' for product 'Ecs'. \n" +
Assert.assertEquals("No endpoint in the region 'mars' for product 'Ecs'.\n" +
"You can set an endpoint for your request explicitly.\n" +
"Or you can use the other available regions: cn-shenzhen cn-beijing ap-south-1 eu-west-1 ap-northeast-1 me-east-1 cn-qingdao cn-shanghai cn-hongkong ap-southeast-1 ap-southeast-2 ap-southeast-3 eu-central-1 cn-huhehaote ap-southeast-5 us-east-1 cn-zhangjiakou us-west-1 cn-hangzhou\n" +
"See https://www.alibabacloud.com/help/zh/doc-detail/92049.htm\n", e.getErrMsg());
"See https://api.alibabacloud.com/product/Ecs", e.getErrMsg());
}
}

Expand Down

0 comments on commit 35caf7c

Please sign in to comment.