Wiki source code of Programming__WebObjects-Web Applications-Development-Custom Error Handling
Version 2.1 by smmccraw on 2007/07/08 09:45
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | == Exception Page == | ||
| 2 | |||
| 3 | To provide a custom error handler when an exception is thrown, override the method: | ||
| 4 | |||
| 5 | {{panel}} | ||
| 6 | |||
| 7 | public WOResponse handleException (java.lang.Exception anException, WOContext aContext) { } | ||
| 8 | |||
| 9 | {{/panel}} | ||
| 10 | |||
| 11 | For example: | ||
| 12 | |||
| 13 | {{panel}} | ||
| 14 | |||
| 15 | public WOResponse handleException(Exception _exception, WOContext _context) { | ||
| 16 | Session session = (Session) _context.session(); | ||
| 17 | // do something to notify user of exception, maybe put a message in the session here ... | ||
| 18 | WOResponse response; | ||
| 19 | if (/* you can't handle exception*/) { | ||
| 20 | response = super.handleException(_exception, _context); | ||
| 21 | } | ||
| 22 | else { | ||
| 23 | response = pageWithName(YourExceptionPage.class.getName(), _context).generateResponse(); | ||
| 24 | } | ||
| 25 | return response; | ||
| 26 | } | ||
| 27 | |||
| 28 | {{/panel}} | ||
| 29 | |||
| 30 | == Session Expired == | ||
| 31 | |||
| 32 | To provide a custom error handler when an exception is thrown, override the method: | ||
| 33 | |||
| 34 | {{panel}} | ||
| 35 | |||
| 36 | public WOResponse handleSessionRestorationErrorInContext(WOContext _context) { | ||
| 37 | |||
| 38 | {{/panel}} | ||
| 39 | |||
| 40 | For example: | ||
| 41 | |||
| 42 | {{panel}} | ||
| 43 | |||
| 44 | |||
| 45 | public WOResponse handleSessionRestorationErrorInContext(WOContext _context) { | ||
| 46 | Session session = (Session) _context.session(); | ||
| 47 | // do something to notify user of exception, maybe put a message in the session here ... | ||
| 48 | WOResponse response = pageWithName(YourErrorPage.class.getName(), _context).generateResponse(); | ||
| 49 | return response; | ||
| 50 | } | ||
| 51 | |||
| 52 | {{/panel}} | ||
| 53 | |||
| 54 | == Your Request Produced an Error == | ||
| 55 | |||
| 56 | === Chuck Hill === | ||
| 57 | |||
| 58 | This message started to appear in WO 5.2 when an exception was raised in a DirectAction, but only in deployed applications. Here is what is happening: | ||
| 59 | |||
| 60 | WODisplayExceptionPages | ||
| 61 | true or false to enable or disable the generation of WOExceptionPages for direct action requests. Default is true in development mode and false in deployment mode. | ||
| 62 | |||
| 63 | From http:~/~/developer.apple.com/documentation/WebObjects/WOAppProperties/AppProperties/chapter_1_section_1.html | ||
| 64 | |||
| 65 | If the page being returned throws during appendToResponse and the app is deployed, all that is displayed is a blank page with the words "Your request produced an error". | ||
| 66 | |||
| 67 | To avoid this, either add this to the launch agruments: | ||
| 68 | |||
| 69 | {{panel}} | ||
| 70 | |||
| 71 | -DWODisplayExceptionPages=true | ||
| 72 | |||
| 73 | {{/panel}} | ||
| 74 | |||
| 75 | Or change the main method in your Application class to look like this: | ||
| 76 | |||
| 77 | {{panel}} | ||
| 78 | |||
| 79 | public static void main(String argv[]) { | ||
| 80 | System.setProperty("WODisplayExceptionPages", "true"); | ||
| 81 | WOApplication.main(argv, Application.class); | ||
| 82 | } | ||
| 83 | |||
| 84 | {{/panel}} | ||
| 85 | |||
| 86 | This won't work if you put it in the constructor. | ||
| 87 | |||
| 88 | Category:WebObjects |