Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Handling Keycloak and Fastapi error messages #52

Open
jonra1993 opened this issue May 30, 2022 · 6 comments
Open

Handling Keycloak and Fastapi error messages #52

jonra1993 opened this issue May 30, 2022 · 6 comments
Labels
documentation Improvements or additions to documentation pull requests welcome An issue that we would like to see fixed, but don't have the time for to fix them personally

Comments

@jonra1993
Copy link
Contributor

jonra1993 commented May 30, 2022

I really appreciate that you created this package. it is something I was looking for too much time ago. I have been using it and I face the following problem I see that the Keycloak exception is fired but it is not passed to fast API which shows 500 error. Do you know how can I pass the error message to Swagger?

This is the API I am trying to use:

@app.post("/users2")
def create_user(first_name: str, last_name: str, email: str, password: SecretStr):
    return idp.create_user(first_name=first_name, last_name=last_name, username=email, email=email, password=password.get_secret_value(), send_email_verification=False)

image

image

@jonra1993 jonra1993 changed the title Handling Keycloak and Fastapi error Handling Keycloak and Fastapi error messages May 30, 2022
@twistfire92
Copy link
Contributor

you can try to override exception handlers

@yannicschroeer
Copy link
Collaborator

yannicschroeer commented Jun 1, 2022

You should define a custom error handler in your FastAPI app for the KeycloakError that catches the error and returns the error message:

@app.exception_handler(KeycloakError)
async def keycloak_exception_handler(request: Request, exc: KeycloakError):
    return JSONResponse(
        status_code=exc.status_code,
        content={"message": exc.reason},
    )

Instead of throwing the error and raising a 500, the app should now proxy the errors as expected.

@jonra1993 Thank you for your question, we're going to add this to the documentation

@yannicschroeer yannicschroeer added the documentation Improvements or additions to documentation label Jun 1, 2022
@jonra1993
Copy link
Contributor Author

Thanks, @yannicschroeer your solution worked as expected

@jonra1993 jonra1993 reopened this Jun 2, 2022
@yannicschroeer yannicschroeer added the pull requests welcome An issue that we would like to see fixed, but don't have the time for to fix them personally label Jun 3, 2022
@jonra1993
Copy link
Contributor Author

Hello, @yannicschroeer I have created a PR to add this into documentation please check it
#54

@Youssef-Harby
Copy link

Youssef-Harby commented Jul 25, 2022

Hello,
Not working for me (create user endpoint as well as keycloak_exception_handler) as I am using router instead of app also tried this

from backend.core.config import settings
from backend.secret import router
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi_keycloak import KeycloakError


def keycloak_exception_handler(request: Request, exc: KeycloakError):
    return JSONResponse(
        status_code=exc.status_code,
        content={"message": exc.reason},
    )

def create_application() -> FastAPI:
    application = FastAPI(title=settings.PROJECT_NAME)
    application.include_router(router)
    return application


app = create_application()
app.add_exception_handler(KeycloakError, keycloak_exception_handler)

You should define a custom error handler in your FastAPI app for the KeycloakError that catches the error and returns the error message:

@app.exception_handler(KeycloakError)
async def keycloak_exception_handler(request: Request, exc: KeycloakError):
    return JSONResponse(
        status_code=exc.status_code,
        content={"message": exc.reason},
    )

Instead of throwing the error and raising a 500, the app should now proxy the errors as expected.

@jonra1993 Thank you for your question, we're going to add this to the documentation

@jonra1993
Copy link
Contributor Author

jonra1993 commented Jul 26, 2022

Hello, @Youssef-Harby can you try this setup using containers it worked in my case I plan in the future migrate this project to AuthN and AuthZ using keycloak it is still a work in progress, but you can check the setup and package versions.

https://github.com/jonra1993/fastapi-alembic-sqlmodel-async/blob/development/fastapi-alembic-sqlmodel-async/app/main.py

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation pull requests welcome An issue that we would like to see fixed, but don't have the time for to fix them personally
Projects
None yet
Development

No branches or pull requests

4 participants