test updates to take into account exception management
This commit is contained in:
@@ -90,7 +90,10 @@ def setup_exception_handlers(app):
|
||||
for error in exc.errors():
|
||||
clean_error = {}
|
||||
for key, value in error.items():
|
||||
if key == 'ctx' and isinstance(value, dict):
|
||||
if key == 'input' and isinstance(value, bytes):
|
||||
# Convert bytes to string representation for JSON serialization
|
||||
clean_error[key] = f"<bytes: {len(value)} bytes>"
|
||||
elif key == 'ctx' and isinstance(value, dict):
|
||||
# Handle the 'ctx' field that contains ValueError objects
|
||||
clean_ctx = {}
|
||||
for ctx_key, ctx_value in value.items():
|
||||
@@ -99,6 +102,9 @@ def setup_exception_handlers(app):
|
||||
else:
|
||||
clean_ctx[ctx_key] = ctx_value
|
||||
clean_error[key] = clean_ctx
|
||||
elif isinstance(value, bytes):
|
||||
# Handle any other bytes objects
|
||||
clean_error[key] = f"<bytes: {len(value)} bytes>"
|
||||
else:
|
||||
clean_error[key] = value
|
||||
clean_errors.append(clean_error)
|
||||
@@ -138,6 +144,23 @@ def setup_exception_handlers(app):
|
||||
}
|
||||
)
|
||||
|
||||
@app.exception_handler(404)
|
||||
async def not_found_handler(request: Request, exc):
|
||||
"""Handle all 404 errors with consistent format."""
|
||||
logger.warning(f"404 Not Found: {request.method} {request.url}")
|
||||
|
||||
return JSONResponse(
|
||||
status_code=404,
|
||||
content={
|
||||
"error_code": "ENDPOINT_NOT_FOUND",
|
||||
"message": f"Endpoint not found: {request.url.path}",
|
||||
"status_code": 404,
|
||||
"details": {
|
||||
"path": request.url.path,
|
||||
"method": request.method
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
# Utility functions for common exception scenarios
|
||||
def raise_not_found(resource_type: str, identifier: str) -> None:
|
||||
|
||||
Reference in New Issue
Block a user