异常处理

系统异常

BOS系统异常提示有如下三种方式:

异常方法 说明
BceHttpClientError 重试时抛出的异常
last_error 最后一次重试时抛出的异常
BceClientError BOS客户端产生的异常
BceInvalidArgumentError 传递参数产生的异常
BceServerError BOS服务器产生的异常

用户可以使用try获取某个事件所产生的异常:

from baidubce import exception
from baidubce.services import bos

try:
    bos_client.delete_object(bucket_name, object_key)
except exception.BceHttpError as e:
    print e.message

返回为:

BceHttpClientError: Unable to execute HTTP request. Retried 0 times. All trace backs:
Traceback (most recent call last):
File "/home/work/python-2.7/lib/python2.7/site-packages/baidubce/http/bce_http_client.py", line 184, in send_request
if handler_function(http_response, response):
File "/home/work/python-2.7/lib/python2.7/site-packages/baidubce/http/handler.py", line 71, in parse_error raise bse
BceServerError: The specified key does not exist.

客户端异常

客户端异常表示客户端尝试向BOS发送请求以及数据传输时遇到的异常。
例如,当发送请求时网络连接不可用时,则会抛出BceHttpClientError;当上传文件时发生IO异常时,也会抛出BceClientError。

服务端异常

当BOS服务端出现异常时,BOS服务端会返回给用户相应的错误信息,以便定位问题。常见服务端异常可参见BOS错误信息格式

参数异常

BOS Python SDK的每个调用都有一些类型固定不可以为空的参数,若该参数传入为空值则返回ValueError,若该参数传入类型错误则返回TypeError。

参数与调用的对应关系如下:

BOS调用参数说明类型
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_uploadsbucket_nameBucket名称string, unicode
put_bucket_loggingsource_bucket,target_bucket源Bucket和目标Bucket名称string, unicode
set_bucket_aclbucket_nameBucket名称string, unicode
aclACL主体包含被授权人及权限list, dict
set_bucket_canned_aclbucket_nameBucket名称string, unicode
canned_aclCannedAcl权限string
put_bucket_lifecyclebucket_nameBucket名称string, unicode
rules生命周期管理规则主体,包含资源及操作动作list, dict
put_bucket_corsbucket_nameBucket名称string, unicode
cors_configuration跨域资源共享(CORS)的规则主体,包含允许的跨域请求的来源及允许的header等list, dict
get_object, get_object_as_string, get_object_meta_data, initiate_multipart_upload, generate_pre_signed_urlbucket_nameBucket名称string, unicode
keyObject名称string
get_object_to_file, put_object_from_filebucket_nameBucket名称string, unicode
keyObject名称string
file_name文件名string
put_object, append_objectbucket_nameBucket名称string, unicode
keyObject名称string
data流对象object
content_length上传Object的大小int, long
content_md5上传Object的MD5string
put_object_from_string,append_object_from_stringbucket_nameBucket名称string, unicode
keyObject名称string
data上传字符串string, unicode
copy_objectsource_bucket_name, target_bucket_name源Bucket和目标Bucket名称string, unicode
source_key, target_key源Object和目标Object名称string
upload_partbucket_name, upload_idBucket名称,标识MultUpload操作全局IDstring, unicode
keyObject名称string
part_number分块编号int
part_size上传分块的大小int, long
part_fp上传分块对象object
upload_part_from_filebucket_name, upload_idBucket名称,标识MultUpload操作全局IDstring, unicode
keyObject名称string
part_number分块编号int
part_size上传分块的大小int, long
file_name文件名string
offset分块的开始偏移位置int
complete_multipart_uploadbucket_name, upload_idBucket名称,标识MultUpload操作全局IDstring, unicode
keyObject名称string
part_list分块列表list
abort_multipart_upload, list_parts, list_all_partsbucket_name, upload_idBucket名称,标识MultUpload操作全局IDstring, unicode
keyObject名称string
delete_multiple_objectsbucket_nameBucket名称string, unicode
key_listobject列表list