Pages

Sunday, September 12, 2010

Custom Error Messages in APEX

As developers we try to prevent unhandled exceptions from occurring for end users. They can occur in any program or language, and APEX is no exception (pardon the pun).

When an unhandled exception happens, users are presented with an error message which is similar to the following:

This error message isn't very user friendly and most users won't know what an "ORA-..." message means. The other issue with this screen is that it does not provide any feedback to developers. If the user does not report this issue developers won't know that it is happening.

As part of my ODTUG presentation I demonstrated how to alter the default error page to provide a user friendly error message and provide an instant notification to developers that an error has occurred. This post will describe how to do implement a user friendly error handling method in APEX 4.0.

- Install LoggerLogger is an open source package written by Tyler Muth. It's an excellent tool to quickly allow developers to instrument their code. Though it is not required, this demo references it. A copy of logger is available here: http://logger.samplecode.oracle.com/.

- Install Simple Modal Plug-inInstall the Simple Modal plugin into your APEX application: http://www.apex-plugin.com/oracle-apex-plugins/dynamic-action-plugin/simple-modal.html. When you download this plugin, the zip file contains 2 plugins. One to show the modal window and one to close the modal window. It is recommended that you install both if you plan to use it in other applications. Only the "Simple Modal - Show" plug is required for this demo.

- Create Error ProcedureCompile this procedure in your schema. It will log all the page items, application items, and all other not-null page items.

- End ResultWhen you have an unhandled exception the end users should see a message like:

You can view all the log information by running the following query:

SELECT * FROM logger_logs WHERE scope = 'apex.demo.unhandeled_exception{session_id: 652754467566839, guid: 901e0663a0896b35e040007f0100049a}'; -- Replace this scope with the scope that is sent in the email

See my description in the Apex forum thread:http://forums.oracle.com/forums/thread.jspa?messageID=820808&#820808820808 .

It’s a pity that for such a long time no neat solution has been made for error handling. I discussed it with Joel and he will take a look at it in the near future to enhance the errorhandling of database errors.

Martin, Nice posting. Ran into an issue with logger installing on a hosting companies site, and was wondering if you had any thoughts on why it was crashing?

(Error returned: ORA-02012: missing USING keyword):

declare– the following line is also used in a constant declaration in logger.pkbl_ctx_name varchar2(35) := substr(sys_context(‘USERENV’,'CURRENT_SCHEMA’),1,23)||’_LOGCTX’;beginexecute immediate ‘create or replace context ‘||l_ctx_name||’ using logger accessed globally’;

Martin, I think it is a good work, I can see it is a little bit slower than other pages with more items and processes. Can you tell this is the best practice for hacking error pages in Apex 4.0, or you can tell there is a better solution. I heard that Apex 4.0 will have a good handling for error page but I can't find where and how.

There is no set standard for hacking the error page in APEX. It all depends on your requirements are for the application.

This page may appear to be slower than other pages since it essentially loads 2 pages. It first goes to the error page, then there's an HTML declaration to immediately go your custom error page. If this is an issue you can handle everything on the error page by modifying the error page template.

APEX 4.0 has some excellent new features on item and page validations but unfortunately, to my knowledge, it still handles unhandled exceptions in the same way as it did in previous versions.

I tested it using : exec logger.log('hello world');select text from logger_logs_5_min;

Then I installed both the Clarifit Simple plugins.

I created your procedure, compiled it with no errors.

I made a page '204' instead of '200' I edited 200 to 204 where necassary.

In my default template (mine seemed to be 'One Level Tabs - Right Sidebar (fixed-width / DIV based)') I copied your javascript into the Error page template area and in the javascript I edited the 200 with 204.

Yes you can do this in APEX 3.2. I actually gave the ODTUG demo using APEX 4.0. Unfortunately I won't be supporting any APEX 3.2 requests for this solution. If you can install the plugins on a 4.0 instance it shouldn't be to hard to implement in 3.2

It is so frustrating that we need some kind of hacks for such a basic functionality. In a page definition should be a 'Error page' option where we can choose to use a default or a custom error display.