Error 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// bosClient is the created BOS Client object
2bucketLocation, err := bosClient.PutBucket("test-bucket")
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("create bucket success, bucket location:", bucketLocation)
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 is generated when BOS server-side errors occur. The service returns detailed error messages to assist troubleshooting. For common server exceptions, refer to [BOS Error Message Format](BOS/API Reference/Error code.md)
