-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Implement uniform error handling and dynamic versioning #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ca892a6
feat: implement uniform error handling and dynamic versioning
aniebietafia fe67f68
feat: implement uniform error handling and dynamic versioning
aniebietafia 0beb3d6
fix: resolve Mypy type errors and Ruff linting issues
aniebietafia ae9dc78
fix: resolve Mypy type errors and Ruff linting issues
aniebietafia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from typing import Any | ||
|
|
||
| from fastapi.responses import JSONResponse | ||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class ErrorDetail(BaseModel): | ||
| field: str | None = None | ||
| message: str | ||
|
|
||
|
|
||
| class ErrorResponse(BaseModel): | ||
| status: str = "error" | ||
| code: str | ||
| message: str | ||
| details: list[ErrorDetail] = [] | ||
|
|
||
|
|
||
| def create_error_response( | ||
| status_code: int, | ||
| code: str, | ||
| message: str, | ||
| details: list[dict[str, Any]] | None = None, | ||
| ) -> JSONResponse: | ||
| """ | ||
| Helper to create a standardized JSON error response. | ||
| """ | ||
| error_details = [] | ||
| if details: | ||
| for detail in details: | ||
| error_details.append( | ||
| ErrorDetail( | ||
| field=detail.get("field"), | ||
| message=detail.get("msg") | ||
| or detail.get("message") | ||
| or "Unknown error", | ||
| ) | ||
| ) | ||
|
|
||
| response_content = ErrorResponse( | ||
| status="error", | ||
| code=code, | ||
| message=message, | ||
| details=error_details, | ||
| ) | ||
|
|
||
| return JSONResponse( | ||
| status_code=status_code, | ||
| content=response_content.model_dump(), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import logging | ||
| from typing import Any | ||
|
|
||
| from fastapi import FastAPI, Request | ||
| from fastapi.exceptions import RequestValidationError | ||
| from fastapi.responses import JSONResponse | ||
| from starlette.exceptions import HTTPException as StarletteHTTPException | ||
|
|
||
| from app.core.error_responses import create_error_response | ||
| from app.core.exceptions import FluentMeetException | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def fluentmeet_exception_handler(_request: Request, exc: Any) -> JSONResponse: | ||
| """ | ||
| Handler for all custom FluentMeetException exceptions. | ||
| """ | ||
| return create_error_response( | ||
| status_code=exc.status_code, | ||
| code=exc.code, | ||
| message=exc.message, | ||
| details=exc.details, | ||
| ) | ||
|
|
||
|
|
||
| async def validation_exception_handler(_request: Request, exc: Any) -> JSONResponse: | ||
| """ | ||
| Handler for Pydantic validation errors (422 -> 400). | ||
| """ | ||
| details = [] | ||
| for error in exc.errors(): | ||
| details.append( | ||
| { | ||
| "field": ".".join(str(loc) for loc in error["loc"]), | ||
| "msg": error["msg"], | ||
| } | ||
| ) | ||
|
|
||
| return create_error_response( | ||
| status_code=400, | ||
| code="VALIDATION_ERROR", | ||
| message="Request validation failed", | ||
| details=details, | ||
| ) | ||
|
|
||
|
|
||
| async def http_exception_handler(_request: Request, exc: Any) -> JSONResponse: | ||
| """ | ||
| Handler for Starlette/FastAPI HTTP exceptions. | ||
| """ | ||
| return create_error_response( | ||
| status_code=exc.status_code, | ||
| code=getattr(exc, "code", "HTTP_ERROR"), | ||
| message=exc.detail, | ||
| ) | ||
|
|
||
|
|
||
| async def unhandled_exception_handler( | ||
| _request: Request, exc: Exception | ||
| ) -> JSONResponse: | ||
| """ | ||
| Handler for all other unhandled exceptions (500). | ||
| """ | ||
| logger.exception("Unhandled exception occurred: %s", str(exc)) | ||
| return create_error_response( | ||
| status_code=500, | ||
| code="INTERNAL_SERVER_ERROR", | ||
| message="An unexpected server error occurred", | ||
| ) | ||
|
|
||
|
|
||
| def register_exception_handlers(app: FastAPI) -> None: | ||
| """ | ||
| Register all custom exception handlers to the FastAPI app. | ||
| """ | ||
| app.add_exception_handler(FluentMeetException, fluentmeet_exception_handler) | ||
| app.add_exception_handler(RequestValidationError, validation_exception_handler) | ||
| app.add_exception_handler(StarletteHTTPException, http_exception_handler) | ||
| app.add_exception_handler(Exception, unhandled_exception_handler) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| from typing import Any | ||
|
|
||
|
|
||
| class FluentMeetException(Exception): | ||
| """ | ||
| Base exception for all FluentMeet API errors. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| status_code: int = 500, | ||
| code: str = "INTERNAL_SERVER_ERROR", | ||
| message: str = "An unexpected error occurred", | ||
| details: list[dict[str, Any]] | None = None, | ||
| ) -> None: | ||
| self.status_code = status_code | ||
| self.code = code | ||
| self.message = message | ||
| self.details = details or [] | ||
| super().__init__(self.message) | ||
|
|
||
|
|
||
| class BadRequestException(FluentMeetException): | ||
| def __init__( | ||
| self, | ||
| message: str = "Bad Request", | ||
| code: str = "BAD_REQUEST", | ||
| details: list[dict[str, Any]] | None = None, | ||
| ) -> None: | ||
| super().__init__(400, code, message, details) | ||
|
|
||
|
|
||
| class UnauthorizedException(FluentMeetException): | ||
| def __init__( | ||
| self, | ||
| message: str = "Unauthorized", | ||
| code: str = "UNAUTHORIZED", | ||
| details: list[dict[str, Any]] | None = None, | ||
| ) -> None: | ||
| super().__init__(401, code, message, details) | ||
|
|
||
|
|
||
| class ForbiddenException(FluentMeetException): | ||
| def __init__( | ||
| self, | ||
| message: str = "Forbidden", | ||
| code: str = "FORBIDDEN", | ||
| details: list[dict[str, Any]] | None = None, | ||
| ) -> None: | ||
| super().__init__(403, code, message, details) | ||
|
|
||
|
|
||
| class NotFoundException(FluentMeetException): | ||
| def __init__( | ||
| self, | ||
| message: str = "Not Found", | ||
| code: str = "NOT_FOUND", | ||
| details: list[dict[str, Any]] | None = None, | ||
| ) -> None: | ||
| super().__init__(404, code, message, details) | ||
|
|
||
|
|
||
| class ConflictException(FluentMeetException): | ||
| def __init__( | ||
| self, | ||
| message: str = "Conflict", | ||
| code: str = "CONFLICT", | ||
| details: list[dict[str, Any]] | None = None, | ||
| ) -> None: | ||
| super().__init__(409, code, message, details) | ||
|
|
||
|
|
||
| class InternalServerException(FluentMeetException): | ||
| def __init__( | ||
| self, | ||
| message: str = "Internal Server Error", | ||
| code: str = "INTERNAL_SERVER_ERROR", | ||
| details: list[dict[str, Any]] | None = None, | ||
| ) -> None: | ||
| super().__init__(500, code, message, details) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: Brints/FluentMeet
Length of output: 318
🏁 Script executed:
head -n 50 app/core/exception_handlers.py | cat -nRepository: Brints/FluentMeet
Length of output: 1796
Fix undefined
Anyannotations to avoid module import/lint failures.Lines 14, 26, and 47 use
Anywithout importing it, which causes F821 pipeline failures and blocks CI.🔧 Proposed fix
Replace
Anywith the concrete exception types that are already imported (FluentMeetException,RequestValidationError,StarletteHTTPException). This provides stronger typing and is already available in the module.🧰 Tools
🪛 GitHub Actions: Code Quality
[error] 14-14: F821 Undefined name 'Any' in type annotation.
[error] 26-26: F821 Undefined name 'Any' in type annotation.
[error] 47-47: F821 Undefined name 'Any' in type annotation.
🤖 Prompt for AI Agents