fastapi-keycloak icon indicating copy to clipboard operation
fastapi-keycloak copied to clipboard

Error on "get_user" when user doesn't exists

Open fabiothz opened this issue 2 years ago • 2 comments

Wen i try to search for a user that doesn't exist, an error occurs:

{
  "detail": "list index out of range"
}

Occurs in the line below, as the API returns a empty array.

https://github.com/code-specialist/fastapi-keycloak/blob/28f5f7bf18c25c8677202ad2f1d3c2437763a58d/fastapi_keycloak/api.py#L833

A suggestion would be to check before instantiating the object:

response_json = response.json()
return KeycloakUser(**response_json[0]) if len(response_json) else None

This way the API will allow me to check if a user exists in keycloak. What do you think?

fabiothz avatar Jun 03 '22 13:06 fabiothz

Hey @fabiothz. Nice catch, this really shouldn't happen.

Personally, I would expect an error to be thrown here. Something like a UserNotFound exception. This would be the most explicit and customizable behavior I can think of. I would wait for @JonasScholl opinion on this

yannicschroeer avatar Jun 03 '22 15:06 yannicschroeer

I believe this can now be closed! @fabiothz Are you still experiencing the issue with the latest release?

stratosgear avatar Oct 14 '22 21:10 stratosgear