davidhamann/python-fmrest

Server instance destructor error after connection error

Closed this issue · 5 comments

Hi David,
Ian and I are including fmrest into the WB2 python project, and it's great to have it.
We are running into a bit of a problem when e.g. the admin forgot to enable the Data API on the server.
In that case fmrest returns an exception with server error 502, which we can trap.
But the destructor of the Server instance has a problem I think. It tries to log out. While it was never able to log in.
This generates another error that I don't know how to trap.

I'm just a noob in Python, but maybe it's a good idea to check in the destructor if the connection was successful.
Thanks, and keep up your great work!

Hi Peter,

I assume you're using the context manager (with)? Would you mind pasting your code snippet and the traceback here?

Thanks for the additional data.

As a workaround you could do the login outside the context manager and then catch the root fmrest exception:

try:
    fms.login()
except fmrest.exceptions.FMRestException as exc:
    print(f"Unable to login via DAPI due to: {exc}")
    return

with fms:
    # ... do your thing as normal; during exit of context manager logout() will be called

But you're right that logout should be a no-op if there's no token in the first place. I'll see if I can make that change in a future version.

From version 1.7.2 you can now catch the BadGatewayError directly:

try:
    fms.login()
    # ...
except fmrest.exceptions.BadGatewayError as exc:
    print(f"Something went wrong: {exc}")

>> Something went wrong: Web server responded with a Bad Gateway error. Check if the Data API is enabled and responding.

Additionally, the logout in the __exit__ will now only be executed when a token exists.