Warning: Can't synchronize with repository "(default)" (/home/git/ome.git does not appear to be a Git repository.). Look in the Trac log for more information.
- Timestamp:
-
02/04/12 23:51:57 (12 years ago)
- Author:
-
wmoore
- Comment:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
-
v1
|
v2
|
|
48 | 48 | you should use appropriate try/except statements. |
49 | 49 | For more info on logging, see [link] |
50 | | For 500 errors, it is a good idea to always return the stack trace along with any message you want the user to see. |
51 | | This allows users to inspect or submit the error for diagnosis etc. |
| 50 | |
| 51 | This is only advised for trivial errors, where you can give the user a simple message, E.g."No Objects selected, Please try again". |
| 52 | |
| 53 | For server errors, it is usually best to allow the exception to be handled by Django since this will provide a lot more info to the user (request details etc) |
| 54 | and format html etc (both with Debug True or False). |
| 55 | If you still want to handle the exception yourself, you can provide stack trace alongside a message for the user. |
| 56 | |
52 | 57 | {{{ |
53 | 58 | try: |
… |
… |
|
56 | 61 | logger.error(traceback.format_exc()) # log the stack trace |
57 | 62 | err_msg = "Something bad happened! \n \n%s" % traceback.format_exc() # message AND stack trace |
58 | | return HttpResponseServerError(err_msg) |
| 63 | if request.is_ajax(): |
| 64 | return HttpResponseServerError(err_msg) |
| 65 | else: |
| 66 | ... # render err_msg with a custom template |
| 67 | return HttpResponseServerError(content) |
59 | 68 | }}} |
60 | 69 | |
61 | 70 | |
| 71 | An alternative is to raise the exception with your custom message, allowing Django to handle it: |
| 72 | |
| 73 | {{{ |
| 74 | |
| 75 | except Exception, x: |
| 76 | logger.error(traceback.format_exc()) # log the stack trace |
| 77 | err_msg = "Something bad happened! \n \n%s" % traceback.format_exc() |
| 78 | raise x.__class__(err_msg) |
| 79 | }}} |
| 80 | |
| 81 | However, the stack trace of the Exception will originate from your raise statement, even though the message of the exception |
| 82 | contains your original stack trace. |
| 83 | |
1.3.13-PRO © 2008-2011
Agilo Software all
rights reserved
(this page was served in: 0.13682 sec.)