AuthorBot commited on
Commit
8471fa1
Β·
1 Parent(s): 131d826

Fix: JSONResponse args reversed causing all exception handlers to crash with TypeError

Browse files
Files changed (2) hide show
  1. app/main.py +12 -11
  2. app/schemas/admin.py +1 -1
app/main.py CHANGED
@@ -216,48 +216,49 @@ def _register_exception_handlers(app: FastAPI) -> None:
216
  @app.exception_handler(Exception)
217
  async def handle_generic(r: Request, e: Exception) -> JSONResponse:
218
  logger.error("Unhandled exception", error=str(e), path=r.url.path, exc_info=True)
219
- return JSONResponse(500, {"detail": "An internal error occurred"})
220
 
221
  @app.exception_handler(AppException)
222
  async def handle_app(r: Request, e: AppException) -> JSONResponse:
223
  logger.error("AppException", error=str(e), path=r.url.path)
224
- return JSONResponse(500, {"detail": str(e)})
225
 
226
  @app.exception_handler(AuthError)
227
  async def handle_auth(r: Request, e: AuthError) -> JSONResponse:
228
- return JSONResponse(401, {"detail": str(e)})
229
 
230
  @app.exception_handler(InvalidTokenError)
231
  async def handle_invalid_token(r: Request, e: InvalidTokenError) -> JSONResponse:
232
- return JSONResponse(401, {"detail": "Invalid or malformed token"})
233
 
234
  @app.exception_handler(ExpiredTokenError)
235
  async def handle_expired_token(r: Request, e: ExpiredTokenError) -> JSONResponse:
236
- return JSONResponse(401, {"detail": "Token has expired"})
237
 
238
  @app.exception_handler(AccountLockedError)
239
  async def handle_locked(r: Request, e: AccountLockedError) -> JSONResponse:
240
- return JSONResponse(423, {"detail": str(e)})
241
 
242
  @app.exception_handler(SubscriptionExpiredError)
243
  async def handle_sub_expired(r: Request, e: SubscriptionExpiredError) -> JSONResponse:
244
- return JSONResponse(403, {"detail": "This chatbot service is currently unavailable"})
245
 
246
  @app.exception_handler(AccessRevokedError)
247
  async def handle_revoked(r: Request, e: AccessRevokedError) -> JSONResponse:
248
- return JSONResponse(403, {"detail": "This chatbot service is currently unavailable"})
249
 
250
  @app.exception_handler(InvalidSubscriptionTokenError)
251
  async def handle_bad_sub_token(r: Request, e: InvalidSubscriptionTokenError) -> JSONResponse:
252
- return JSONResponse(401, {"detail": "Invalid or expired subscription token"})
253
 
254
  @app.exception_handler(SubscriptionNotFoundError)
255
  async def handle_no_sub(r: Request, e: SubscriptionNotFoundError) -> JSONResponse:
256
- return JSONResponse(403, {"detail": "No active subscription. Contact support."})
257
 
258
  @app.exception_handler(BudgetExhaustedError)
259
  async def handle_budget(r: Request, e: BudgetExhaustedError) -> JSONResponse:
260
- return JSONResponse(200, {"message": "I'm taking a short break to recharge! Check back soon."})
 
261
 
262
 
263
  def _register_routers(app: FastAPI) -> None:
 
216
  @app.exception_handler(Exception)
217
  async def handle_generic(r: Request, e: Exception) -> JSONResponse:
218
  logger.error("Unhandled exception", error=str(e), path=r.url.path, exc_info=True)
219
+ return JSONResponse(status_code=500, content={"detail": "An internal error occurred"})
220
 
221
  @app.exception_handler(AppException)
222
  async def handle_app(r: Request, e: AppException) -> JSONResponse:
223
  logger.error("AppException", error=str(e), path=r.url.path)
224
+ return JSONResponse(status_code=500, content={"detail": str(e)})
225
 
226
  @app.exception_handler(AuthError)
227
  async def handle_auth(r: Request, e: AuthError) -> JSONResponse:
228
+ return JSONResponse(status_code=401, content={"detail": str(e)})
229
 
230
  @app.exception_handler(InvalidTokenError)
231
  async def handle_invalid_token(r: Request, e: InvalidTokenError) -> JSONResponse:
232
+ return JSONResponse(status_code=401, content={"detail": "Invalid or malformed token"})
233
 
234
  @app.exception_handler(ExpiredTokenError)
235
  async def handle_expired_token(r: Request, e: ExpiredTokenError) -> JSONResponse:
236
+ return JSONResponse(status_code=401, content={"detail": "Token has expired"})
237
 
238
  @app.exception_handler(AccountLockedError)
239
  async def handle_locked(r: Request, e: AccountLockedError) -> JSONResponse:
240
+ return JSONResponse(status_code=423, content={"detail": str(e)})
241
 
242
  @app.exception_handler(SubscriptionExpiredError)
243
  async def handle_sub_expired(r: Request, e: SubscriptionExpiredError) -> JSONResponse:
244
+ return JSONResponse(status_code=403, content={"detail": "This chatbot service is currently unavailable"})
245
 
246
  @app.exception_handler(AccessRevokedError)
247
  async def handle_revoked(r: Request, e: AccessRevokedError) -> JSONResponse:
248
+ return JSONResponse(status_code=403, content={"detail": "This chatbot service is currently unavailable"})
249
 
250
  @app.exception_handler(InvalidSubscriptionTokenError)
251
  async def handle_bad_sub_token(r: Request, e: InvalidSubscriptionTokenError) -> JSONResponse:
252
+ return JSONResponse(status_code=401, content={"detail": "Invalid or expired subscription token"})
253
 
254
  @app.exception_handler(SubscriptionNotFoundError)
255
  async def handle_no_sub(r: Request, e: SubscriptionNotFoundError) -> JSONResponse:
256
+ return JSONResponse(status_code=403, content={"detail": "No active subscription. Contact support."})
257
 
258
  @app.exception_handler(BudgetExhaustedError)
259
  async def handle_budget(r: Request, e: BudgetExhaustedError) -> JSONResponse:
260
+ return JSONResponse(status_code=200, content={"message": "I'm taking a short break to recharge! Check back soon."})
261
+
262
 
263
 
264
  def _register_routers(app: FastAPI) -> None:
app/schemas/admin.py CHANGED
@@ -6,7 +6,7 @@ R-010: Password change MUST validate via Pydantic schema.
6
 
7
  from typing import Literal
8
 
9
- from pydantic import BaseModel, EmailStr, Field, HttpUrl, field_validator
10
 
11
 
12
  # ─── Authentication ────────────────────────────────────────────────────────────
 
6
 
7
  from typing import Literal
8
 
9
+ from pydantic import BaseModel, EmailStr, Field, field_validator
10
 
11
 
12
  # ─── Authentication ────────────────────────────────────────────────────────────