Reputation: 65
I'm developing my own template for errors pages, but the handler500
don't get the exception parameter, and I can't know what is the cause of error.
How can I do this? My custom template already is working, but I need the information about the exception cause. My handler500
:
def error_500(request):
template_name = "error_500.html"
error_code = "500"
url = request.build_absolute_uri()
username = request.user.username
useruuid = request.user.uuid
date = datetime.datetime.now()
errorObject = Errors(
error_code=error_code, url=url,
username=username, useruuid=useruuid,
date=date
)
errorObject.save()
errorUUID = errorObject.uuid
context = {
"errorUUID": errorUUID
}
return render(request, template_name, context)
Upvotes: 3
Views: 434
Reputation: 5854
You can use sys.exc_info()
import sys;
def error_500(request):
...
type_, value, traceback = sys.exc_info()
...
From Python Docs:
This function returns a tuple of three values that give information about the exception that is currently being handled. The information returned is specific both to the current thread and to the current stack frame. If the current stack frame is not handling an exception, the information is taken from the calling stack frame, or its caller, and so on until a stack frame is found that is handling an exception. Here, “handling an exception” is defined as “executing an except clause.” For any stack frame, only information about the exception being currently handled is accessible.
If no exception is being handled anywhere on the stack, a tuple containing three None values is returned. Otherwise, the values returned are (type, value, traceback
).
Upvotes: 1