Exception handling
The Go language uses the error type to identify errors. BOS supports two types of errors as shown in the table below:
| Error type | Description |
|---|---|
| BceClientError | Errors caused by user operations |
| BceServiceError | Errors returned by the BOS service |
When users invoke BOS-related APIs through the SDK, in addition to obtaining the required results, they will also receive errors if any occur, which can be retrieved and handled. An example is as follows:
1// EIP_CLIENT is the created EIP client object
2err := EIP_CLIENT.DeleteEip(EIP_ADDRESS, getClientToken())
3if err != nil {
4 switch realErr := err.(type) {
5 case *bce.BceClientError:
6 fmt.Println("client occurs error:", realErr.Error())
7 case *bce.BceServiceError:
8 fmt.Println("service occurs error:", realErr.Error())
9 default:
10 fmt.Println("unknown error:", err)
11 }
12} else {
13 fmt.Println("delete eip success")
14}
Client exception
A client exception arises when the client faces issues while sending requests or transferring data to BOS. For instance, network failures or I/O errors during file uploads will result in a BceClientError.
Server exception
A server exception occurs when there are errors on the Baidu AI Cloud server side. The service will return detailed error messages to help with troubleshooting. For details on specific server-side exceptions, consult the official documentation of the respective service.
SDK logging
The GO SDK features an independently implemented logging module that supports six levels of logging, three output destinations (standard output, standard error, and file), and basic format settings. Its import path is github.com/baidubce/bce-sdk-go/util/log. When outputting logs to a file, it supports five log rolling modes (no rolling, rolling by day, rolling by hour, rolling by minute, and rolling by size). In such cases, the directory for output log files must also be specified.
This logging module does not rely on any external dependencies. Developers working with the GO SDK can directly integrate this logging module into their projects. They can either use the package-level log object provided by the GO SDK or create a new log object. Refer to the examples below for details:
1// Directly use the package-level global log object (will be output together with the GO SDK’s own logs)
2log.SetLogHandler(log.STDERR)
3log.Debugf("%s", "logging message using the log package in the sdk")
4 // Create a new log object (output logs according to custom settings, separated from the GO SDK’s log output)
5myLogger := log.NewLogger()
6myLogger.SetLogHandler(log.FILE)
7myLogger.SetLogDir("/home/log")
8myLogger.SetRotateType(log.ROTATE_SIZE)
9myLogger.Info("this is my own logger from the sdk")
