Exception handling
EIP exception prompts are available in the following two ways:
| Exception type | Description |
|---|---|
| BceClientException | Client exception |
| BceServerException | Server exception |
Users can use try to obtain the exception generated by a certain event, e.g.:
1try {
2 eipClient.listEips();
3 } catch (BceServiceException bce){
4 System.out.println(bce.getMessage());
5 } catch (BceClientException bce){
6 System.out.println(bce.getMessage());
7 }
Client exception
A client exception occurs when the client faces issues during request transmission or file uploads to EIP. For example, a ClientException is thrown in cases of network failures or I/O errors.
Server exception
A server exception is generated when EIP server-side errors occur. The service returns detailed error messages to assist troubleshooting. For common server exceptions, refer to EIP Error Code
SDK logging
The EIP Java SDK package uses logback as its default SLF4J implementation. If users do not have their own logging implementations, logback can be used directly. Alternatively, other logging frameworks like log4j can also be employed.
Default logging
To utilize the default logback, include a logback.xml configuration file in the classpath. If this file is missing, the log level defaults to DEBUG.
1<configuration>
2 <property name="LOG_HOME" value="./log/"/>
3 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4 <!-- encoders are assigned the type
5 ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
6 <encoder>
7 <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
8 </encoder>
9 </appender>
10 <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
11 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
12 <FileNamePattern>${LOG_HOME}/EipUnitTest.%d{yyyy-MM-dd}.log</FileNamePattern>
13 <MaxHistory>30</MaxHistory>
14 </rollingPolicy>
15 <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
16 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
17 </encoder>
18 <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
19 <MaxFileSize>10MB</MaxFileSize>
20 </triggeringPolicy>
21 </appender>
22 <root level="info">
23 <appender-ref ref="STDOUT"/>
24 <appender-ref ref="FILE"/>
25 </root>
26</configuration>
Custom logging modules
For projects already equipped with a logging implementation, logback can be excluded via Maven (pom.xml).
1<?xml version="1.0" encoding="utf-8"?>
2<dependency>
3 <groupId>com.baidubce</groupId>
4 <artifactId>bce-java-sdk</artifactId>
5 <version>${bce.sdk.version}</version>
6 <exclusions>
7 <exclusion>
8 <groupId>ch.qos.logback</groupId>
9 <artifactId>logback-classic</artifactId>
10 </exclusion>
11 <exclusion>
12 <groupId>ch.qos.logback</groupId>
13 <artifactId>logback-core</artifactId>
14 </exclusion>
15 <exclusion>
16 <groupId>org.slf4j</groupId>
17 <artifactId>jcl-over-slf4j</artifactId>
18 </exclusion>
19 </exclusions>
20</dependency>
