Tuesday, September 15, 2009

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.
Post a Comment