|
2 | 2 | import logging |
3 | 3 | import re |
4 | 4 |
|
5 | | -from fastapi import Depends, FastAPI, status |
| 5 | +from fastapi import Depends, FastAPI, HTTPException, Request, status |
| 6 | +from fastapi.exceptions import RequestValidationError |
| 7 | +from fastapi.responses import JSONResponse |
| 8 | +from pydantic import ValidationError |
6 | 9 |
|
7 | 10 | from feast import FeatureStore |
8 | 11 | from feast.api.registry.rest import register_all_routes |
| 12 | +from feast.errors import ( |
| 13 | + FeastObjectNotFoundException, |
| 14 | + FeastPermissionError, |
| 15 | + PushSourceNotFoundException, |
| 16 | +) |
9 | 17 | from feast.permissions.auth.auth_manager import get_auth_manager |
10 | 18 | from feast.permissions.server.rest import inject_user_details |
11 | 19 | from feast.permissions.server.utils import ( |
@@ -62,11 +70,113 @@ def __init__(self, store: FeatureStore): |
62 | 70 | "X-Frame-Options": "DENY", |
63 | 71 | }, |
64 | 72 | ) |
| 73 | + self._add_exception_handlers() |
65 | 74 | self._add_logging_middleware() |
66 | 75 | self._add_openapi_security() |
67 | 76 | self._init_auth() |
68 | 77 | self._register_routes() |
69 | 78 |
|
| 79 | + def _add_exception_handlers(self): |
| 80 | + """Add custom exception handlers to include HTTP status codes in JSON responses.""" |
| 81 | + |
| 82 | + @self.app.exception_handler(HTTPException) |
| 83 | + async def http_exception_handler(request: Request, exc: HTTPException): |
| 84 | + return JSONResponse( |
| 85 | + status_code=exc.status_code, |
| 86 | + content={ |
| 87 | + "status_code": exc.status_code, |
| 88 | + "detail": exc.detail, |
| 89 | + "error_type": "HTTPException", |
| 90 | + }, |
| 91 | + ) |
| 92 | + |
| 93 | + @self.app.exception_handler(FeastObjectNotFoundException) |
| 94 | + async def feast_object_not_found_handler( |
| 95 | + request: Request, exc: FeastObjectNotFoundException |
| 96 | + ): |
| 97 | + return JSONResponse( |
| 98 | + status_code=404, |
| 99 | + content={ |
| 100 | + "status_code": 404, |
| 101 | + "detail": str(exc), |
| 102 | + "error_type": "FeastObjectNotFoundException", |
| 103 | + }, |
| 104 | + ) |
| 105 | + |
| 106 | + @self.app.exception_handler(FeastPermissionError) |
| 107 | + async def feast_permission_error_handler( |
| 108 | + request: Request, exc: FeastPermissionError |
| 109 | + ): |
| 110 | + return JSONResponse( |
| 111 | + status_code=403, |
| 112 | + content={ |
| 113 | + "status_code": 403, |
| 114 | + "detail": str(exc), |
| 115 | + "error_type": "FeastPermissionError", |
| 116 | + }, |
| 117 | + ) |
| 118 | + |
| 119 | + @self.app.exception_handler(PushSourceNotFoundException) |
| 120 | + async def push_source_not_found_handler( |
| 121 | + request: Request, exc: PushSourceNotFoundException |
| 122 | + ): |
| 123 | + return JSONResponse( |
| 124 | + status_code=422, |
| 125 | + content={ |
| 126 | + "status_code": 422, |
| 127 | + "detail": str(exc), |
| 128 | + "error_type": "PushSourceNotFoundException", |
| 129 | + }, |
| 130 | + ) |
| 131 | + |
| 132 | + @self.app.exception_handler(ValidationError) |
| 133 | + async def validation_error_handler(request: Request, exc: ValidationError): |
| 134 | + return JSONResponse( |
| 135 | + status_code=422, |
| 136 | + content={ |
| 137 | + "status_code": 422, |
| 138 | + "detail": str(exc), |
| 139 | + "error_type": "ValidationError", |
| 140 | + }, |
| 141 | + ) |
| 142 | + |
| 143 | + @self.app.exception_handler(RequestValidationError) |
| 144 | + async def request_validation_error_handler( |
| 145 | + request: Request, exc: RequestValidationError |
| 146 | + ): |
| 147 | + return JSONResponse( |
| 148 | + status_code=422, |
| 149 | + content={ |
| 150 | + "status_code": 422, |
| 151 | + "detail": str(exc), |
| 152 | + "error_type": "RequestValidationError", |
| 153 | + }, |
| 154 | + ) |
| 155 | + |
| 156 | + @self.app.exception_handler(ValueError) |
| 157 | + async def value_error_handler(request: Request, exc: ValueError): |
| 158 | + return JSONResponse( |
| 159 | + status_code=422, |
| 160 | + content={ |
| 161 | + "status_code": 422, |
| 162 | + "detail": str(exc), |
| 163 | + "error_type": "ValueError", |
| 164 | + }, |
| 165 | + ) |
| 166 | + |
| 167 | + @self.app.exception_handler(Exception) |
| 168 | + async def generic_exception_handler(request: Request, exc: Exception): |
| 169 | + logger.error(f"Unhandled exception: {exc}", exc_info=True) |
| 170 | + |
| 171 | + return JSONResponse( |
| 172 | + status_code=500, |
| 173 | + content={ |
| 174 | + "status_code": 500, |
| 175 | + "detail": f"Internal server error: {str(exc)}", |
| 176 | + "error_type": "InternalServerError", |
| 177 | + }, |
| 178 | + ) |
| 179 | + |
70 | 180 | def _add_logging_middleware(self): |
71 | 181 | from fastapi import Request |
72 | 182 | from starlette.middleware.base import BaseHTTPMiddleware |
|
0 commit comments