Skip to content

Add exception handlers for backend#332

Open
FxJYg wants to merge 9 commits intomainfrom
felix/add_exception_handlers
Open

Add exception handlers for backend#332
FxJYg wants to merge 9 commits intomainfrom
felix/add_exception_handlers

Conversation

@FxJYg
Copy link
Contributor

@FxJYg FxJYg commented Mar 9, 2025

Purpose

Closes #371

New Changes

Added exception handlers for both custom and common exceptions.

Testing

Added unit testing for all the exception handling.

Outstanding Changes

N/A

@github-actions
Copy link

github-actions bot commented Mar 9, 2025

Pull reviewers stats

Stats of the last 120 days for UWOrbital:

User Total reviews Time to review Total comments
kepler452b123 24 2d 3h 49m 88
Yarik-Popov 13 1d 53m 114

⚡️ Pull request stats

@Yarik-Popov Yarik-Popov changed the title Felix/add exception handlers Add exception handlers for backend Mar 9, 2025
Copy link
Contributor

@Yarik-Popov Yarik-Popov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic is overall correct. Some minor issues with the code that should be easy to fix.

Also, instead of hardcoding the status codes that in the future we will forget what the meaning of them are. You should import the HTTP status codes fastapi.status

Copy link
Contributor

@Yarik-Popov Yarik-Popov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added one suggestion to try to fix the previous issue that existed.

Also, can you write some unit tests for this. See the python_test directory for examples on existing tests. You can find some hints on how to test APIs in the gs-onboarding repo

@Yarik-Popov
Copy link
Contributor

The only thing that is left are the tests. Once those are finished and they are good, you should be done. Great job so far.

@Yarik-Popov Yarik-Popov added the project: backend Backend tasks label Mar 22, 2025
Copy link
Contributor

@Yarik-Popov Yarik-Popov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall the tests are good. There are some small improvements to them that are needed.

Comment on lines +130 to +133
raise HTTPException(
status_code=HTTP_400_BAD_REQUEST,
detail=exc.message,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait is this one the only one is a HTTPException while all the other ones are JSONResponse?

Comment on lines +65 to +108
# Test BaseOrbitalError
response = fastapi_test_client.get("/test_base_orbital_exceptions")
assert response.status_code == HTTP_400_BAD_REQUEST
assert response.json().get("message") == "Test BaseOrbitalError"

# Test DatabaseError
response = fastapi_test_client.get("/test_database_exceptions")
assert response.status_code == HTTP_400_BAD_REQUEST
assert response.json().get("message") == "Test DatabaseError"

# Test InvalidArgumentError
response = fastapi_test_client.get("/test_invalid_argument_exceptions")
assert response.status_code == HTTP_422_UNPROCESSABLE_ENTITY
assert response.json().get("message") == "Test InvalidArgumentError"

# Test InvalidStateError
response = fastapi_test_client.get("/test_invalid_state_exceptions")
assert response.status_code == HTTP_409_CONFLICT
assert response.json().get("message") == "Test InvalidStateError"

# Test NotFoundError
response = fastapi_test_client.get("/test_not_found_exceptions")
assert response.status_code == HTTP_404_NOT_FOUND
assert response.json().get("message") == "Test NotFoundError"

# Test ServiceError
response = fastapi_test_client.get("/test_service_exceptions")
assert response.status_code == HTTP_400_BAD_REQUEST
assert response.json().get("message") == "Test ServiceError"

# Test SunPositionError
response = fastapi_test_client.get("/test_sun_position_exceptions")
assert response.status_code == HTTP_400_BAD_REQUEST
assert response.json().get("message") == "Test SunPositionError"

# Test UnauthorizedError
response = fastapi_test_client.get("/test_unauthorized_exceptions")
assert response.status_code == HTTP_401_UNAUTHORIZED
assert response.json().get("message") == "Test UnauthorizedError"

# Test UnknownError
response = fastapi_test_client.get("/test_unknown_exceptions")
assert response.status_code == HTTP_400_BAD_REQUEST
assert response.json().get("detail") == "Test UnknownError"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally its best not to have lots of different asserts in the same asserts in the same test. If one of the tests fail at the beginning, the tests after will not run and you will lose important contex. Have a look at the test_ephemeris.py for examples on how to use pytest.mark.parameterize

Comment on lines +25 to +64
app = FastAPI()
setup_exception_handlers(app)

@app.get("/test_base_orbital_exceptions")
async def test_custom_exceptions():
raise BaseOrbitalError("Test BaseOrbitalError")

@app.get("/test_database_exceptions")
async def test_database_exceptions():
raise DatabaseError("Test DatabaseError")

@app.get("/test_invalid_argument_exceptions")
async def test_invalid_argument_exceptions():
raise InvalidArgumentError("Test InvalidArgumentError")

@app.get("/test_invalid_state_exceptions")
async def test_invalid_state_exceptions():
raise InvalidStateError("Test InvalidStateError")

@app.get("/test_not_found_exceptions")
async def test_not_found_exceptions():
raise NotFoundError("Test NotFoundError")

@app.get("/test_service_exceptions")
async def test_service_exceptions():
raise ServiceError("Test ServiceError")

@app.get("/test_sun_position_exceptions")
async def test_sun_position_exceptions():
raise SunPositionError("Test SunPositionError")

@app.get("/test_unauthorized_exceptions")
async def test_unauthorized_exceptions():
raise UnauthorizedError("Test UnauthorizedError")

@app.get("/test_unknown_exceptions")
async def test_unknown_exceptions():
raise UnknownError("Test UnknownError")

fastapi_test_client = TestClient(app)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you extract this out to a function that creates the needed exception handler test client? Something such as create_exception_handler_client just above it would be great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

project: backend Backend tasks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add exception handlers

2 participants