27 lines
732 B
Python
27 lines
732 B
Python
"""Safe error responses for API trust boundaries."""
|
|
import logging
|
|
from uuid import uuid4
|
|
|
|
from fastapi import HTTPException
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def internal_error(context: str, error: Exception) -> HTTPException:
|
|
"""Log only a non-sensitive error classification and return a safe response."""
|
|
error_id = uuid4().hex
|
|
logger.error(
|
|
"%s failed [error_id=%s, error_type=%s]",
|
|
context,
|
|
error_id,
|
|
type(error).__name__,
|
|
)
|
|
return HTTPException(
|
|
status_code=500,
|
|
detail={
|
|
"code": "INTERNAL_ERROR",
|
|
"message": "服务器内部错误,请联系管理员并提供错误编号",
|
|
"error_id": error_id,
|
|
},
|
|
)
|