GlobusSDK Checking if a Collection exists

Skip to first unread message

Joshua Brown

May 20, 2022, 3:21:57 PMMay 20
to Discuss
This might already be possible with the existing GlobusSDK, but I wanted to check to be sure, is it possible to check if a collection exists from the GlobusSDK without throwing an error? It would be nice if I could get back the response as a dict that is easily parsable rather than an exception.

Stephen Rosen

May 20, 2022, 3:42:12 PMMay 20
to Joshua Brown, Discuss
Hi Joshua,

It's not possible to disable the error-raising behavior, but it is possible to parse data out of GlobusAPIError and its subclasses.

I believe you may want something like this:

    client = GCSClient(...)
        data = client.get_collection(collection_id)
    except globus_sdk.GlobusAPIError as e:
        if e.http_status == 404:
            data = e.raw_json

Any response in the 4xx or 5xx category (e.g. 404, 500) is treated as an error. This helps us guarantee that code like

    myclient = ClientClassFoo(...)

will error if the underlying API call fails.

There are also interesting implications for type annotations and response parsing from this behavior.

I notice that `raw_json` is missing from the narrative documentation for exceptions, but the documentation does have API doc covering all of the available attributes of the exception objects:

I'll see about adding an example to the narrative block at the start which uses `raw_json` to parse an error response.


Joshua Brown

May 20, 2022, 4:10:27 PMMay 20
to Discuss,, Discuss, Joshua Brown
That will work, thanks!
Reply all
Reply to author
0 new messages