Skip to content

Global exception handling#212

Merged
AkhileshNegi merged 26 commits intomainfrom
enhancement/global_exception
Jun 16, 2025
Merged

Global exception handling#212
AkhileshNegi merged 26 commits intomainfrom
enhancement/global_exception

Conversation

@nishika26
Copy link
Copy Markdown
Collaborator

@nishika26 nishika26 commented Jun 10, 2025

Summary

Target issue is #125
Explain the motivation for making this change. What existing problem does the pull request solve?
This PR introduces a centralized exception handling mechanism and integrates it across all relevant routes to ensure consistent error responses across the platform. This PR removes redundant try/except blocks across multiple routes and CRUD layers, replacing them with consistent exception propagation which is handled globally (wiith @app.exception_handler)

@codecov
Copy link
Copy Markdown

codecov bot commented Jun 10, 2025

@nishika26 nishika26 marked this pull request as ready for review June 11, 2025 07:40
@nishika26 nishika26 requested review from AkhileshNegi and avirajsingh7 and removed request for AkhileshNegi and avirajsingh7 June 11, 2025 07:53
@nishika26 nishika26 requested review from AkhileshNegi, Ishankoradia and avirajsingh7 and removed request for AkhileshNegi June 12, 2025 07:44
api_key = session.get(APIKey, api_key_id)

if not api_key or api_key.is_deleted:
raise ValueError("API key not found or already deleted")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we should avoid raising HTTPException in the crud layer. Use ValueError or a custom exception instead, and convert it to an HTTPException in the API route.
It’s a data layer; it shouldn't know about HTTP.
Also this is reusable method and may be used some where else in application.

@AkhileshNegi or @Ishankoradia Can confirm on this?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I am not fussy about either.

If we do raise ValueError, we should avoid catching and re-raising in the API route, since the global exception handler (Exception) is already doing this. In such cases the status code will mostly be 500, if we do want to send a specific status code, then re-raising makes sense (or in such cases, raising HttpException also makes sense)


app.include_router(api_router, prefix=settings.API_V1_STR)

app.add_exception_handler(HTTPException, http_exception_handler)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we should remove http_exception_handler completely if not needed.
Please remove this from app/api/deps.py also.

Copy link
Copy Markdown
Collaborator

@Ishankoradia Ishankoradia Jun 12, 2025

Choose a reason for hiding this comment

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

Might seem like a redundant step. But i see 2 benefits of doing this

  1. A standard APi response for failures if we already dont have one. Also if we want to change this structure in the future, it becomes very easy
  2. In future if we integrate any observability tool, it becomes very easy to send (integrate) the error logs from this one place to the monitoring tool

What do you think ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sorry but I’m not fully clear on points you mentioned.

The http_exception_handler(this function was old one do not get confused with new function) was implemented to convert all HTTP exceptions into a standardized format. However, since we are already using a global exception handler that achieves the same goal, we don’t need the http_exception_handler.

Copy link
Copy Markdown
Collaborator

@Ishankoradia Ishankoradia Jun 14, 2025

Choose a reason for hiding this comment

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

Which part you didn't understand ?

I will leave it up to you guys to take the final call. 😄 @AkhileshNegi

api_key = session.get(APIKey, api_key_id)

if not api_key or api_key.is_deleted:
raise ValueError("API key not found or already deleted")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I am not fussy about either.

If we do raise ValueError, we should avoid catching and re-raising in the API route, since the global exception handler (Exception) is already doing this. In such cases the status code will mostly be 500, if we do want to send a specific status code, then re-raising makes sense (or in such cases, raising HttpException also makes sense)


app.include_router(api_router, prefix=settings.API_V1_STR)

app.add_exception_handler(HTTPException, http_exception_handler)
Copy link
Copy Markdown
Collaborator

@Ishankoradia Ishankoradia Jun 12, 2025

Choose a reason for hiding this comment

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

Might seem like a redundant step. But i see 2 benefits of doing this

  1. A standard APi response for failures if we already dont have one. Also if we want to change this structure in the future, it becomes very easy
  2. In future if we integrate any observability tool, it becomes very easy to send (integrate) the error logs from this one place to the monitoring tool

What do you think ?

@nishika26 nishika26 self-assigned this Jun 16, 2025
@nishika26 nishika26 linked an issue Jun 16, 2025 that may be closed by this pull request
@nishika26 nishika26 added the enhancement New feature or request label Jun 16, 2025
@nishika26 nishika26 moved this to In Progress in Kaapi-dev Jun 16, 2025
@AkhileshNegi AkhileshNegi merged commit 06a47c4 into main Jun 16, 2025
2 checks passed
@AkhileshNegi AkhileshNegi deleted the enhancement/global_exception branch June 16, 2025 11:31
@github-project-automation github-project-automation bot moved this from In Progress to Closed in Kaapi-dev Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: Closed

Development

Successfully merging this pull request may close these issues.

Global Exception handlers

4 participants