MushroomMaula / fastapi_login

FastAPI-Login tries to provide similar functionality as Flask-Login does.
https://pypi.org/project/fastapi-login
MIT License
639 stars 58 forks source link

TypeError: catching classes that do not inherit from BaseException is not allowed #47

Closed kigawas closed 3 years ago

kigawas commented 3 years ago

It happens when the token is not correct, for example, a random string token.

INFO:     127.0.0.1:51577 - "POST /auth/refresh HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 369, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 59, in __call__
    return await self.app(scope, receive, send)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/fastapi/applications.py", line 199, in __call__
    await super().__call__(scope, receive, send)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc from None
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/routing.py", line 580, in __call__
    await route.handle(scope, receive, send)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/routing.py", line 241, in handle
    await self.app(scope, receive, send)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/routing.py", line 52, in app
    response = await func(request)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/fastapi/routing.py", line 206, in app
    solved_result = await solve_dependencies(
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/fastapi/dependencies/utils.py", line 548, in solve_dependencies
    solved = await call(**sub_values)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/fastapi_login/fastapi_login.py", line 346, in __call__
    if not self.has_scopes(token, security_scopes):
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/fastapi_login/fastapi_login.py", line 308, in has_scopes
    except self.not_authenticated_exception as e:
TypeError: catching classes that do not inherit from BaseException is not allowed
kigawas commented 3 years ago

I see. The reason is that self.not_authenticated_exception or InvalidCredentialsException is an exception object which can only be raised but not caught.

image

MushroomMaula commented 3 years ago

Thanks a lot for catching this. I will merge your pull request soon and publish a fixed version.