Exception handling
System exceptions
BOS system exception prompts are available in the following three ways:
| Exception type | Description |
|---|---|
| BceHttpClientError | Exceptions thrown during retry |
| last_error | Exceptions thrown at last retry |
| BceClientError | Exceptions generated by the BOS client |
| BceInvalidArgumentError | Exceptions generated by parameter transmission |
| BceServerError | Exceptions generated by the BOS server |
Users can use try to obtain the exception generated by a certain event:
1from baidubce import exception
2from baidubce.services import bos
3try:
4 bos_client.delete_object(bucket_name, object_key)
5except exception.BceHttpClientError as e:
6 print("status code: ", e.status_code)
7 print("code: ", e.code)
8 print("error message: ", e.last_error)
9 print("request id: ", e.request_id)
10 print("BceHttpClientError: ", e)
Response:
1status code: 404
2code: NoSuchKey
3error message: The specified key does not exist.
4request id: 3c22ae79-0aad-42dd-b900-5bb53497a49d
5BceHttpClientError: Unable to execute HTTP request. Retried 0 times. All trace backs:
6Traceback (most recent call last):
7File "/home/work/python-2.7/lib/python2.7/site-packages/baidubce/http/bce_http_client.py", line 184, in send_request
8if handler_function(http_response, response):
9File "/home/work/python-2.7/lib/python2.7/site-packages/baidubce/http/handler.py", line 71, in parse_error raise bse
10BceServerError: The specified key does not exist.
Client exception
A client exception occurs when the client encounters issues while sending requests or transmitting data to BOS. For example, BceHttpClientError is thrown for network failures or I/O errors during file uploads.
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)
Parameter exceptions
Every function in the BOS Python SDK requires certain parameters of fixed types that cannot be null. Passing a null value to such a parameter will result in a ValueError, while providing an incorrect type will result in a TypeError.
The corresponding relationship between parameters and calls is as follows:
| BOS invocation | Parameters | Description | Types |
|---|---|---|---|
| create_bucket, does_bucket_exist, delete_bucket, get_bucket_location, get_bucket_lifecycle, delete_bucket_lifecycle, get_bucket_cors, delete_bucket_cors, get_bucket_logging, delete_bucket_logging, list_objects, list_all_objects, delete_object, get_bucket_acl, list_multipart_uploads, list_all_multipart_uploads | bucket_name | Bucket name | string, unicode |
| put_bucket_logging | source_bucket, target_bucket | Source bucket and target bucket name | string, unicode |
| set_bucket_acl | bucket_name | Bucket name | string, unicode |
| acl | ACL subject, including the authorized persons and permissions | list, dict | |
| set_bucket_canned_acl | bucket_name | Bucket name | string, unicode |
| canned_acl | CannedAcl permissions | string | |
| put_bucket_lifecycle | bucket_name | Bucket name | string, unicode |
| rules | Lifecycle management rule subject, including resources and operation actions | list, dict | |
| put_bucket_cors | bucket_name | Bucket name | string, unicode |
| cors_configuration | Cross-origin resource sharing (CORS) rule subject, including allowed sources of cross-origin requests and allowed headers | list, dict | |
| get_object, get_object_as_string, get_object_meta_data, initiate_multipart_upload, generate_pre_signed_url | bucket_name | Bucket name | string, unicode |
| key | Object name | string | |
| get_object_to_file, put_object_from_file | bucket_name | Bucket name | string, unicode |
| key | Object name | string | |
| file_name | File name | string | |
| put_object, append_object | bucket_name | Bucket name | string, unicode |
| key | Object name | string | |
| data | Stream object | object | |
| content_length | Upload object size | int, long | |
| content_md5 | Upload object MD5 | string | |
| put_object_from_strin, append_object_from_string | bucket_name | Bucket name | string, unicode |
| key | Object name | string | |
| data | Upload string | string, unicode | |
| copy_object | source_bucket_name, target_bucket_name | Source bucket and target bucket name | string, unicode |
| source_key, target_key | Names of source object and target object | string | |
| upload_part | bucket_name, upload_id | Bucket name, identifying the global ID of the MultUpload operation | string, unicode |
| key | Object name | string | |
| part_number | Part number | int | |
| part_size | Uploaded part size | int, long | |
| part_fp | Uploaded part object | object | |
| upload_part_from_file | bucket_name, upload_id | Bucket name, identifying the global ID of the MultUpload operation | string, unicode |
| key | Object name | string | |
| part_number | Part number | int | |
| part_size | Uploaded part size | int, long | |
| file_name | File name | string | |
| offset | Starting offset position of the part | int | |
| complete_multipart_upload | bucket_name, upload_id | Bucket name, identifying the global ID of the MultUpload operation | string, unicode |
| key | Object name | string | |
| part_list | Part list | list | |
| abort_multipart_upload, list_parts, list_all_parts | bucket_name, upload_id | Bucket name, identifying the global ID of the MultUpload operation | string, unicode |
| key | Object name | string | |
| delete_multiple_objects | bucket_name | Bucket name | string, unicode |
| key_list | Object list | list |
