Skip to content

Commit

Permalink
fix: different custom exception definition
Browse files Browse the repository at this point in the history
  • Loading branch information
wiwski committed Apr 15, 2024
1 parent 9c5dbe2 commit fbb62cf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
15 changes: 0 additions & 15 deletions exceptions.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,2 @@
from fastapi import Request, status
from fastapi.responses import JSONResponse


class NoProjectMembershipException(Exception):
pass


# pylint: disable=unused-argument
async def no_project_membership_exception_handler(
request: Request,
exc: NoProjectMembershipException,
):
return JSONResponse(
status_code=status.HTTP_403_FORBIDDEN,
content={"detail": "User does not have access to this project"},
)
25 changes: 16 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import os

import sentry_sdk
from fastapi import FastAPI
from fastapi import FastAPI, Request, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse

from api import config, connect, data, deployments, hdf5, infra, vms
from exceptions import (
NoProjectMembershipException,
no_project_membership_exception_handler,
)
from exceptions import NoProjectMembershipException


sentry_sdk.init(
dsn=os.getenv("SENTRY_DSN"),
Expand All @@ -18,10 +17,6 @@

app = FastAPI()

app.add_exception_handler(
NoProjectMembershipException, no_project_membership_exception_handler
)

app.add_middleware(
CORSMiddleware,
allow_origins=os.getenv("CORS_ALLOWED_ORIGIN", "").split(" "),
Expand All @@ -37,3 +32,15 @@
app.include_router(config.router)
app.include_router(infra.router)
app.include_router(hdf5.router)


@app.exception_handler(NoProjectMembershipException)
# pylint: disable=unused-argument
async def no_project_membership_exception_handler(
request: Request,
exc: NoProjectMembershipException,
):
return JSONResponse(
status_code=status.HTTP_403_FORBIDDEN,
content={"detail": "User does not have access to this project"},
)

0 comments on commit fbb62cf

Please sign in to comment.