错误处理
更新时间:2019-11-15
异常处理
TableStorage异常有如下两种:
异常方法 | 说明 |
---|---|
BceClientException | 客户端异常,客户端异常表示客户端尝试向TableStorage发送请求以及数据传输时遇到的异常。 例如,当输入不合法时,会抛出ClientException;当发送请求时网络连接不可用时,也会抛出 ClientException。 |
BceServerException | 服务器异常,当TableStorage服务端出现异常时,TableStorage服务端会返回给用户相应的错误信息,以便定位问题。常见服务端异常可参见错误码。 |
用户可以使用try获取某个事件所产生的异常,例如:
TableStorageClient client = new TableStorageClient(conf, INSTANCE_NAME, false);
ListTablesResponse response = null;
try{
response = client.listTables(new ListTablesRequest());
} catch (BceServiceException bse) {
System.out.println("Caught an tablestorage, which means your request made it "
+ "to tablestorage, but was rejected with an error responseContent for some reason.");
System.out.println("Error Message: " + bse.getMessage());
System.out.println("HTTP ErrorDescription Code: " + bse.getStatusCode()); //获取http状态码
System.out.println("tablestorage Error Code: " + bse.getErrorCode()); //获取TableStorage错误码
System.out.println("tablestorage Error Message: " + bse.getErrorMessage()); //获取TableStorage错误信息
System.out.println("Error Type: " + bse.getErrorType()); //获取错误类型
System.out.println("Request ID: " + bse.getRequestId()); //获取request id
bse.printStackTrace();
} catch (BceClientException bce) {
System.out.println("Caught an TableStorageClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with TableStorage, "
+ "such as not being able to access the network.");
System.out.println("Error Message: " + bce.getMessage());
bce.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
client.shutdown();
}
SDK日志配置
logback配置
TableStorage Java SDK发布版本中使用logback作为slf4j的实现,使用时需要在classpath中配置logback.xml,详细内容可以参考如下配置。如果没有这个配置文件,日志级别默认为DEBUG。
<configuration>
<property name="LOG_HOME" value="./log/"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/TableStorageUnitTest.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<root level="info">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
自有日志配置
如果需要使用其他实现(如log4j),可以在pom.xml中采用以下配置来去除logback。
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>bce-java-sdk</artifactId>
<version>${bce.sdk.version}</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>