RelationalAI / rai-sdk-python

The RelationalAI Software Development Kit (SDK) for Python.
Apache License 2.0
17 stars 4 forks source link

Error handling at the api.X level #11

Closed tomasrelai closed 3 years ago

tomasrelai commented 3 years ago

Cloning into an existing database, say, gives an HTTP error. This is a little obscure, wondering if we should give higher-level errors. This would help narrow down the error --- maybe the target already exists, or maybe the source DB does not exist, etc.

  File "/Users/uribe/relai/git/tomas/python/clone_test.py", line 6, in <module>
    rsp = api.create_database(ctx, "clonetest1", "MYENGINE", overwrite=False, source="clonetest")
  File "/Users/uribe/relai/git/rai-sdk-python/virtualenv/lib/python3.9/site-packages/rai_sdk-0.5.0-py3.9.egg/railib/api.py", line 250, in create_database
  File "/Users/uribe/relai/git/rai-sdk-python/virtualenv/lib/python3.9/site-packages/rai_sdk-0.5.0-py3.9.egg/railib/api.py", line 197, in run
  File "/Users/uribe/relai/git/rai-sdk-python/virtualenv/lib/python3.9/site-packages/rai_sdk-0.5.0-py3.9.egg/railib/rest.py", line 247, in post
  File "/Users/uribe/relai/git/rai-sdk-python/virtualenv/lib/python3.9/site-packages/rai_sdk-0.5.0-py3.9.egg/railib/rest.py", line 230, in request
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 523, in open
    response = meth(req, response)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 632, in http_response
    response = self.parent.error(
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 561, in error
    return self._call_chain(*args)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 494, in _call_chain
    result = func(*args)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 641, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 422: Unprocessable Entity
tomasrelai commented 3 years ago

(Same for creating a database that already exists using overwrite=False, etc.)

tomasrelai commented 3 years ago

In the examples (but not all) we do this:

def show_error(e: HTTPError) -> None:
    r = e.read()
    if len(r) > 0:
        rsp = json.loads(r)
        print(f"Got error, status: {e.status}")
        print(json.dumps(rsp, indent=2))
    else:
        print("There is no descriptive error message, got:", e)

def run(profile: str):
    cfg = config.read(profile=profile)
    ctx = api.Context(**cfg)
    try:
        rsp = api.list_databases(ctx)
        print(json.dumps(rsp, indent=2))
    except HTTPError as e:
        show_error(e)

We could add this to all the examples and mention it in the docs.

bradlo commented 3 years ago

renamed local show_error to http_error, moved to railib.show, and added handlers to all examples so they will produce meaningful outputs from the response payload.

billscheidel-rai commented 1 year ago

Note: This issue has been migrated to https://relationalai.atlassian.net/browse/RAI-3780.

This link is only accessible to employees of RelationalAI.