如何在python中解析AzureHttpError

2024-04-26 12:18:31 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我从azure获得的exception的内容(如果类型为exceptionazure.common.AzureHttpError在python中):

Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. <?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:a8ecea4d-001e-0112-76b0-9a717b000000 Time:2018-01-31T16:31:20.9812336Z</Message><AuthenticationErrorDetail>Signature did not match. String to sign used was masking_it_intentionally rwdlacup bfqt sco 2018-01-03T21:19:36Z 2018-03-09T05:19:36Z https 2017-04-17 </AuthenticationErrorDetail></Error>

异常的主体似乎是一些字符串,后面是xml样式的信息块。我想从xml数据中提取一个特定的键,例如AuthenticationErrorDetail(这样我就可以根据它的值编写一些代码)

我想用最简单的方法来完成,也就是说,我不想自己解析异常体。因为如果我决定自己解析xml,我将如何决定xml从哪里开始呢?(是的,我可以搜索字符串<?xml,但我想避免它)。在

那么,我该怎么做呢?在

我希望已经有一些库/工具/解析器/对象来实现这一点。在


Tags: ofthetomakeserverisvaluerequest
1条回答
网友
1楼 · 发布于 2024-04-26 12:18:31

AzureHttpError有一些可以使用的属性:status_codeerror_code和{}。前两个可能对您的目的最有用。我检查这些是我所期望的,否则将返回错误。在

try:
    # try something
except AzureHttpError as err:

    if err.status_code == 409 and err.error_code == 'BlobAlreadyExists':
                print('Blob already exists.')
            else:
                raise err

我不知道这是否是最好的方法,我在文档中找不到任何东西,所以使用err.__dict__err.__dir__()来解决问题,但它确实有效,而且肯定比解析错误字符串要好。在

相关问题 更多 >