没有捕获Python异常?

时间:2019-10-02 15:06:48

标签: python exception try-catch

我在代码中设置了一个try catch,但是看来我的异常是不正确的,因为它似乎没有捕获到它。

我正在使用模块中的异常,也许我没有正确导入它?这是我的代码:

import logging
import fhirclient.models.bundle as b
from fhirclient.server import FHIRUnauthorizedException

logging.disable(logging.WARNING)

def get_all_resources(resource, struct, smart):
    '''Perform a search on a resource type and get all resources entries from all retunred bundles.\n
    This function takes all paginated bundles into consideration.'''  

    if smart.ready == False:
        smart.reauthorize

    search = resource.where(struct)
    bundle = search.perform(smart.server)

    resources = [entry.resource for entry in bundle.entry or []]

    next_url = _get_next_url(bundle.link)

    while next_url != None:
        try:
            json_dict = smart.server.request_json(next_url)
        except FHIRUnauthorizedException:
            smart.reauthorize
            continue

        bundle = b.Bundle(json_dict)
        resources += [entry.resource for entry in bundle.entry or []]
        next_url = _get_next_url(bundle.link)

    return resources

现在,当我运行代码时,出现以下错误:

Traceback (most recent call last):
  File "code.py", line 79, in <module>
    main()
  File "code.py", line 42, in main
    reports = get_all_resources(dr.DiagnosticReport, search, smart)
  File "somepath/fhir_tools/resource.py", line 23, in get_all_resources
    json_dict = smart.server.request_json(next_url)
  File "/usr/local/lib/python3.6/dist-packages/fhirclient/server.py", line 153, in request_json
    res = self._get(path, headers, nosign)
  File "/usr/local/lib/python3.6/dist-packages/fhirclient/server.py", line 181, in _get
    self.raise_for_status(res)
  File "/usr/local/lib/python3.6/dist-packages/fhirclient/server.py", line 256, in raise_for_status
    raise FHIRUnauthorizedException(response)
server.FHIRUnauthorizedException: <Response [401]>

我的例外不应该抓住这个吗?

0 个答案:

没有答案