14.6.4 Handling Errors in a Common Way
Centralize error lookup, reporting, and logging so pages and APIs handle errors consistently.
Use a package like
APP_COMMON below to centralize application-wide
common error handling logic. This simplifies reporting and logging errors from a single
location. Key functions include:
error_message– to return an error message in the current language based on a Static IDreport_if_error– to optionally raise an error if the message passed in is not nullreport_error_key– to report an error in the current language based on a Static ID
package app_common is
c_workspace constant varchar2(40) := 'COMPANION';
c_app_id constant number := 1001;
-----------------------------------------------------------
-- Return error message with indicated key
-----------------------------------------------------------
function error_message(
p_message_key in varchar2,
p_params in apex_t_varchar2 default null)
return varchar2;
-----------------------------------------------------------
-- Report error message with indicated behavior if non-null
-----------------------------------------------------------
procedure report_if_error(
p_error_message in varchar2);
-----------------------------------------------------------
-- Report error message with indicated message key
-----------------------------------------------------------
procedure report_error_key(
p_error_message_key in varchar2,
p_params in apex_t_varchar2 default null);
end app_common;The APP_COMMON package body appears below. Notice its is_apex_call that detects whether the error is being reported while running in the context of an APEX application or not. The report_if_error procedure uses this to decide whether to add the error to the APEX error stack using APEX_ERROR.ADD_ERROR, or whether to raise an exception containing the error message instead. The code uses APEX_LANG.GET_MESSAGE to get the error message by Static ID in the user's current language.
package body app_common is
--------------------------------------------
function is_apex_call
return boolean
is
begin
return apex_application.g_flow_id is not null;
end is_apex_call;
--------------------------------------------
function error_message(
p_message_key in varchar2,
p_params in apex_t_varchar2 default null)
return varchar2
is
begin
apex_util.set_workspace(c_workspace);
return apex_lang.get_message(
p_name => p_message_key,
p_params => p_params,
p_application_id => c_app_id);
end error_message;
--------------------------------------------
procedure report_if_error(
p_error_message in varchar2)
is
l_error varchar2(4000);
begin
if p_error_message is not null then
if is_apex_call then
apex_error.add_error (
p_message => p_error_message,
p_display_location => apex_error.c_on_error_page);
else
raise_application_error(-20001,p_error_message);
end if;
end if;
end report_if_error;
-----------------------------------------------
procedure report_error_key(
p_error_message_key in varchar2,
p_params in apex_t_varchar2 default null)
is
begin
report_if_error(
p_error_message => error_message(p_error_message_key,p_params));
end report_error_key;
end app_common;Parent topic: Enforcing Business Logic in REST APIs