Exception handling
When an SDK call fails, the result information is included in the bos_status_t structure returned by the API.
The recommended usage of each API in the SDK is as follows. For simplicity, the examples in this document do not include specific exception handling but only illustrate the API usage.
1bos_status_t *s = NULL;
2s = bos_put_object_from_file(options, &bucket, &object, &file, &headers, &resp_headers);
3if (!s && !bos_status_is_ok(s)) {
4 // Perform log output and handling for exception scenarios as needed
5 bos_warn_log("failed to put object from file", buf);
6 if (s->error_code) bos_warn_log("status->error_code: %s", s->error_code);
7 if (s->error_msg) bos_warn_log("status->error_msg: %s", s->error_msg);
8 if (s->req_id) bos_warn_log("status->req_id: %s", s->req_id);
9}
Client exception
A client exception occurs when the client encounters issues while sending requests or transmitting data to BOS. For example, when the network connection is unavailable during request sending, the client operation will return non-zero; when an IO exception occurs during file upload, it will also return non-zero.
If the value of the code member in the bos_status_t structure is less than 0, it signifies a local client error in the SDK. For details about the error codes, refer to the definition of the enumeration bos_error_code_e.
1typedef enum {
2 BOSE_OK = 0,
3 BOSE_OUT_MEMORY = -1000,
4 BOSE_OVER_MEMORY = -999,
5 BOSE_FAILED_CONNECT = -998,
6 BOSE_ABORT_CALLBACK = -997,
7 BOSE_INTERNAL_ERROR = -996,
8 BOSE_REQUEST_TIMEOUT = -995,
9 BOSE_INVALID_ARGUMENT = -994,
10 BOSE_INVALID_OPERATION = -993,
11 BOSE_CONNECTION_FAILED = -992,
12 BOSE_FAILED_INITIALIZE = -991,
13 BOSE_NAME_LOOKUP_ERROR = -990,
14 BOSE_FAILED_VERIFICATION = -989,
15 BOSE_WRITE_BODY_ERROR = -988,
16 BOSE_READ_BODY_ERROR = -987,
17 BOSE_SERVICE_ERROR = -986,
18 BOSE_OPEN_FILE_ERROR = -985,
19 BOSE_FILE_SEEK_ERROR = -984,
20 BOSE_FILE_INFO_ERROR = -983,
21 BOSE_FILE_READ_ERROR = -982,
22 BOSE_FILE_WRITE_ERROR = -981,
23 BOSE_JSON_PARSE_ERROR = -975,
24 BOSE_UTF8_ENCODE_ERROR = -979,
25 BOSE_CRC_INCONSISTENT_ERROR = -978,
26 BOSE_FILE_FLUSH_ERROR = -977,
27 BOSE_FILE_TRUNC_ERROR = -976,
28 BOSE_UNKNOWN_ERROR = -100
29} bos_error_code_e;
Server exception
If the value of the code member in the bos_status_t structure is greater than 0, it indicates an error occurred on the network side.
The following is a description of the bos_status_t structure:
| Members of bos_status_t | Description | Types |
|---|---|---|
| code | The status code of the response. A 4xx code means the request failed due to the client, and a 5xx code means the failure was caused by a server-side exception | Int |
| error_code | The Error Code returned in the body when the request fails | String |
| error_msg | The Error Message returned in the body when the request fails | String |
| req_id | The request ID, which is used to identify a user’s unique request | String |
