python config_error ai_generated true

AttributeError: 'HTTPException' object has no attribute 'detail'

ID: python/fastapi-http-exception-handler-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2025-11-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

自定义异常处理器中错误地访问了 `exc.detail`,但该属性不存在或名称错误。

generic

中文

自定义异常处理器中错误地访问了 `exc.detail`,但该属性不存在或名称错误。

Workarounds

  1. 95% success
    在异常处理器中使用 `exc.detail` 获取错误信息,如 `return JSONResponse(status_code=exc.status_code, content={"detail": exc.detail})`。
  2. 90% success
    确保导入 `from fastapi import HTTPException` 并使用正确的属性。

Dead Ends

Common approaches that don't work:

  1. 70% fail

    尝试使用 `exc.message` 但实际属性为 `detail`,导致 AttributeError。

  2. 50% fail

    在异常处理器中未正确导入 HTTPException,导致类型不匹配。