Reputation: 1
I’m working on an Oracle APEX application where I use custom PL/SQL functions to handle business logic. These functions can encounter errors, and I want to display these errors on the page (e.g., as inline messages or notifications) instead of raising exceptions with RAISE_APPLICATION_ERROR.
I’m not sure how to pass these errors from my PL/SQL function into APEX without stopping the process (as RAISE_APPLICATION_ERROR does).
How can return custom error messages from PL/SQL functions and display them on the page?
I've tried it before:
create or replace function APEX_ERROR_CUSTOM
(
p_error IN apex_error.t_error
)
RETURN apex_error.t_error_result
IS
l_result apex_error.t_error_result := apex_error.t_error_result();
c_inline_with_field constant varchar2(40):='INLINE_WITH_FIELD';
c_inline_in_notification constant varchar2(40):='INLINE_IN_NOTIFICATION';
BEGIN
l_result := apex_error.init_error_result ( p_error => APEX_ERROR_CUSTOM.p_error );
-- only remove ORA error for user defined exceptions
IF p_error.ora_sqlcode BETWEEN -20999 AND -20000 THEN
l_result.message := REPLACE(l_result.message,'ORA'||p_error.ora_sqlcode||': ','');
END IF;
RETURN l_result;
END APEX_ERROR_CUSTOM;
/
and it didn't work
Upvotes: 0
Views: 41
Reputation: 18695
I wouldn't use a custom error function for this - that doesn't change the behaviour of APEX. Instead, in each page process, write a block around the pl/sql call that you expect to error and trap the error using APEX_ERROR.ADD_ERROR
to show it on the page. Here is a very basic example of a pl/sql block that will throw an error.
DECLARE
l_num NUMBER;
BEGIN
l_num := 'x';
-- try to use a named exception, not when 'OTHERS'
EXCEPTION WHEN VALUE_ERROR THEN
APEX_ERROR.ADD_ERROR (
p_message => 'Some error message',
p_additional_info => 'Additional Info',
p_display_location => apex_error.c_inline_in_notification
);
END;
The APEX_ERROR API (docs) contains a number of functions to manage error in the your application.
Upvotes: 0