G-5080
🆓Warning
Always use FORMAT_ERROR_BACKTRACE when using FORMAT_ERROR_STACK or SQLERRM.
Reason
In exception handler sqlerrm and format_error_stack won't tell you the exact line where the error occurred. format_error_backtrace displays the call stack at the point where an exception was raised, even if the subprogram is called from an exception handler in an outer scope.
If you use sqlerrm or format_error_stack to log/display error, you should also include format_error_backtrace to identify the exact location where the exception was raised.
Example
Non-Compliant Example
create or replace package body order_api as
procedure discount_and_recalculate(
in_customer_id in integer
,in_discount in number
) is
co_customer_id constant customer.id%type := in_customer_id;
co_discount constant customer.discount_percentage%type := in_discount;
begin
customer_api.apply_discount(
in_customer_id => co_customer_id
,in_discount => co_discount
);
customer_api.calc(co_customer_id);
exception
when zero_divide then
null; -- ignore
when others then
logging_package.log_error('Error: ' || sqlerrm);
raise;
end discount_and_recalculate;
end order_api;
/Issues
| Line | Column | Message |
|---|---|---|
| 18 | 49 |
Compliant Solution - ★★★★★
create or replace package body order_api as
procedure discount_and_recalculate(
in_customer_id in integer
,in_discount in number
) is
co_customer_id constant customer.id%type := in_customer_id;
co_discount constant customer.discount_percentage%type := in_discount;
begin
customer_api.apply_discount(
in_customer_id => co_customer_id
,in_discount => co_discount
);
customer_api.calc(co_customer_id);
exception
when zero_divide then
null; -- ignore
when others then
logging_package.log_error(
'Error: '
|| sqlerrm
|| ' - Backtrace: '
|| sys.dbms_utility.format_error_backtrace
);
raise;
end discount_and_recalculate;
end order_api;
/Parameters
Use parameters to customize the rule to your needs.
| Parameter | Description | Default Value |
|---|---|---|
| BacktraceProcedureCalls | Comma-separated list of PL/SQL package procedures that ensure the result of sys.dbms_utility.format_error_backtrace is logged. | dbl_log_api.log_error |
| DisableAllQuickFix | Comma-separated list of rules for which a quick fix should not be applied to all the problems in a file. | Core G-3130 |
References
- same as Trivadis G-5080
- similar to plsql:FormatErrorStackAndBacktraceUsedAlongside
The scope of plsql:FormatErrorStackAndBacktraceUsedAlongside is limited to FORMAT_ERROR_BACKTRACE and FORMAT_ERROR_STACK and ignores SQLERRM.