dbt-cloud-cli incorrect API token causes unhelpful Python language error

The problem I’m having

A TypeError: 'NoneType' object is not subscriptable error is returned when an incorrect API token is passed into dbt-cloud-cli

The context of why I’m trying to do this

I ran dbt-cloud-cli with the wrong API token, however, the unhelpful error meant it was very difficult to determine that.

What I’ve already tried

I made a local change that replaced these lines in cli.py;

With;

    if wait:
        if not response.ok:
            import json
            raise DbtCloudException(f"Job run failed. Response: {json.dumps(response.json(), indent=2)}")

        run_id = response.json()["data"]["id"]

Which gives a more helpful error;

dbt_cloud.exc.DbtCloudException: Job run failed. Response: {
  "status": {
    "code": 403,
    "is_success": false,
    "user_message": "Access denied: User API Keys are deprecated. Please use account scoped PAT.",
    "developer_message": null
  },
  "data": null
}

I also created this issue, but I’m not sure if that repo is regularly checked;

Some example code or error messages

To reproduce the error, replace thexxxxxxs below with valid values and run the following;

python -m venv .venv
source .venv/bin/activate
pip install dbt-cloud-cli
DBT_CLOUD_API_TOKEN="BADTOKEN" DBT_CLOUD_ACCOUNT_ID="xxxxxx" DBT_CLOUD_ENV_ID="xxxxxx" DBT_CLOUD_JOB_ID="xxxxxx" dbt-cloud job run --git-branch "xxxxxx" --wait --cause "Bad API token testing"

Result;


Traceback (most recent call last):
  File "/tmp/test/.venv/bin/dbt-cloud", line 8, in <module>
    sys.exit(dbt_cloud())
             ^^^^^^^^^^^
  File "/tmp/test/.venv/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/test/.venv/lib/python3.11/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/tmp/test/.venv/lib/python3.11/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/test/.venv/lib/python3.11/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/test/.venv/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/test/.venv/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/test/.venv/lib/python3.11/site-packages/dbt_cloud/cli.py", line 123, in run
    run_id = response.json()["data"]["id"]
             ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
TypeError: 'NoneType' object is not subscriptable