Python: Getting the error message of an exception

后端 未结 4 1481
孤独总比滥情好
孤独总比滥情好 2021-01-03 17:40

In python 2.6.6, how can I capture the error message of an exception.

IE:

response_dict = {} # contains info to response under a django view.
try:
           


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-03 18:43

    You should use unicode instead of string if you are going to translate your application.

    BTW, Im case you're using json because of an Ajax request, I suggest you to send errors back with HttpResponseServerError rather than HttpResponse:

    from django.http import HttpResponse, HttpResponseServerError
    response_dict = {} # contains info to response under a django view.
    try:
        plan.save()
        response_dict.update({'plan_id': plan.id})
    except IntegrityError, e: #contains my own custom exception raising with custom messages.
        return HttpResponseServerError(unicode(e))
    
    return HttpResponse(json.dumps(response_dict), mimetype="application/json")
    

    and then manage errors in your Ajax procedure. If you wish I can post some sample code.

提交回复
热议问题