Skip to main content

Transform ORA-xxxx messages into something more user friendly in APEX

When you delete a record that is referenced using a foreign key constraint you get a rather ugly error like this:
Of course you can prevent such a thing from happening by creating a Page process that checks if your delete is "Ok". But that's double coding...and dangerous when you have to maintain your database structure: You'll probably forget to update the processes as well. Wouldn't it be nice if you can 'catch' that error an present it to the end user in a more friendly way (and with a somewhat more understandable text)? I guess your users will be more enthusiastic about your application (and you) when you show them something like this:
So how do we achieve that?

1. Edit the "Error Page Template Control" region of your Application Default Page Template, because that's the one that is used for presenting errors. Set it to :

<script type="text/javascript">$(document).ready(function(){raiseErrorHandler();});</script>
2. Create the raiseErrorHandler function (I'll use some jQuery UI stuff to show the nice dialog box):

function raiseErrorHandler(){
vError = $(".ErrorPageMessage");
vError.hide();
var get = new htmldb_Get( null, $v('pFlowId')
,'APPLICATION_PROCESS=GetErrorMessage'
,$v('pFlowStepId'));
get.addParam( 'x01', vError.html());
gReturn = get.get();
get = null;
var errArray = gReturn.split("#",2);
showError( vError, errArray[0], errArray[1]);
}

function showError(pThis, pTitle, pText){
vText = '
'+'
'+pText+'
'
$(pThis).append(vText);
$("#alert").dialog({
bgiframe: true,
modal: true,
minHeight : 200,
width : 600,
closeOnEscape : false,
close : function(){window.history.go(-1)},
buttons: {
Ok: function() {
$(this).dialog('close');
$("#alert").remove();
window.history.go(-1);
}}
});
}
3. That Javascript function calls an Application Process GetErrorMessage:
DECLARE
p_error_title varchar2(32767);
p_error_text varchar2(32767);
p_ora_error varchar2(32767);
BEGIN
p_ora_error := wwv_flow.g_x01;
-- Get "translated" error message
get_error_message( p_ora_error, p_error_title, p_error_text );
htp.p( p_error_title||'#'|| p_error_text );
END;
4. The procedure get_error_message is defined in the database. This procedure queries a table that contains the contraint name (the 'ORA Error') and a more user friendly description.

create or replace PROCEDURE GET_ERROR_MESSAGE
( p_ora_error IN VARCHAR2
, p_error_title OUT VARCHAR2
, p_error_text OUT VARCHAR2
) AS
CURSOR c_mps IS
SELECT code, description
FROM afk_message_properties
WHERE INSTR( p_ora_error, constraint_name ) > 0;
BEGIN
OPEN c_mps;
FETCH c_mps INTO p_error_title, p_error_text;
CLOSE c_mps;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
p_error_title := 'No translation found for this error';
p_error_text := p_ora_error;
END GET_ERROR_MESSAGE;
The Application Process spits out a concatenated text, that will be segregated again in the Javascript function. The result is presented to the user in a way that would make him happy....
Even if there is no error definition found in the table, the error will be shown in the dialog box - only the translation to a normal language is missing.

Kinda cool I think!
You can see a public demo when you click here.
38 comments

Popular posts from this blog

Showing a success message after closing a modal dialog

APEX 5 comes with Modal Dialogs out of the box. Very neat. Especially for adding and changing data. And to minimise the number of time a user has to click, it could be useful to add a "Close Dialog" process after the actual data processing. When the data processing fails, the Dialog stays on top showing the error. When data processing runs fine, the Dialog is closed ... without any confirmation. And this might be scary for a shaky user.

So how can we provide the user some feedback? On Page 4 of the Sample Dialog Application you can see one solution: up on a Dialog Closed Event on the parent page it does a redirect to refresh the parent page appending the success message of the "Close Dialog" process. This has two drawbacks. First, it probably refreshes more than necessary. And second, if you're using multiple layers of dialogs (dialogs that open other dialogs) the message appears in the "parent dialog".
As an alternative you could follow these steps: 1…

It's happening again ... running for the ODTUG Board of Directors 😉

For the third time in a row I'll be running for ODTUG's Board of Directors. But after ending as a runner up twice, I am sure I'm going to make it this time! But not without your help!

My campaign statement this year is:
I have been attending and presenting at Kscope conferences since 2007. This not only resulted in a vast amount of knowledge, but also - and even more important - a huge number of friends from all over the globe.  I want to see ODTUG grow and spread this community feeling even more! 
My experience as an attendee, presenter and content lead has provided the basic foundation to be a director. Next to that, my personality and (global) network will be beneficial to the whole board and organization. 
Since March I have served on the Board of Directors in a limited term for a Director who stepped down due to a career change. This has allowed me to have unique insight of all the things that are going on in and around the ODTUG organization. As the train was already ro…

APEX 5 New Static File Features

In APEX 4 you could upload files - like CSS files, JavaScript files, Images and whatever else you like - into the APEX Repository. When you navigate to Shared Components, there is a Files section that offers three different options:
CSS Files are always uploaded (and changed !) for the whole Workspace. For Images and Static Files (usually JavaScript) you could choose whether they should be available for the whole Workspace or for a specific Application only. And if you had a lot of files - e.g. a lot of images - then you had to go through the upload process one-by-one. But that's usually a one time only thing. If you make changes to the CSS and JavaScript files - and that's a continuous process in development - then you had to delete the existing file and upload the new one. Over and over again. And meanwhile fighting the cache of the webserver and your browser.  And another irritating issue: You couldn't use relative references in your CSS or JavaScript files as they just…