title: FastApi : traceback.exc_format return none when using add_exception_handler
body: <p>I develop with FastApi, and want to contain traceback info in response when error occur;<br>
  To do so, I define exception handlers in <code>exception_handler.py</code> :</p>
  <pre class="lang-py prettyprint-override"><code>from fastapi.responses import JSONResponse
  from fastapi import status
  from fastapi import FastAPI, Request
  from traceback import format_exc, print_exc
  
  
  def general_exception_handler(req: Request, exc: Exception):
      '''
      Exception handler for unspecified exceptions 
      '''
      
      tracback_msg = format_exc() 
      return JSONResponse(
          {
              &quot;code&quot;: status.HTTP_500_INTERNAL_SERVER_ERROR,
              &quot;message&quot;: f&quot;error info: {tracback_msg}&quot;,
              # &quot;message&quot;: f&quot;error info: {str(exc)}&quot;,
              &quot;data&quot;: &quot;&quot;,
          },
          status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
      )
  
  </code></pre>
  <p>And attach those handler to fastappi app instance in <code>server.py</code>:</p>
  <blockquote>
  <p><code>server.py</code> is where I create app instance and attach extra function to it like middlewares or exception handlers.</p>
  </blockquote>
  <pre class="lang-py prettyprint-override"><code>from core import router # api routers are defined in router.py
  from fastapi import FastAPI
  from core.exception_handler import general_exception_handler
  
  
  app = FastAPI(
      debug=False,
      docs_url=None,
      redoc_url=None
  )
  
  
  # attach exception handler to app instance
  app.add_exception_handler(Exception, general_exception_handler)
  
  # include routers to app intance
  app.include_router(router.router)
  </code></pre>
  <p>The problem is, when exception was raised, traceback message return by <code>format_exc()</code> is <code>None</code>;<br>But when I used <code>str(exc)</code> like the annotated code, I got the exception info properly but of course without traceback info.</p>