Giter VIP home page Giter VIP logo

Comments (6)

yannicschroeer avatar yannicschroeer commented on June 6, 2024 2

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

from fastapi-keycloak.

twistfire92 avatar twistfire92 commented on June 6, 2024

you can try to override exception handlers

from fastapi-keycloak.

jonra1993 avatar jonra1993 commented on June 6, 2024

Thanks, @yannicschroeer your solution worked as expected

from fastapi-keycloak.

jonra1993 avatar jonra1993 commented on June 6, 2024

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

from fastapi-keycloak.

Youssef-Harby avatar Youssef-Harby commented on June 6, 2024

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

from fastapi-keycloak.

jonra1993 avatar jonra1993 commented on June 6, 2024

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

from fastapi-keycloak.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.