rspec/rules/S2458/plsql/rule.adoc

71 lines
1.5 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Centralizing the definitions of custom exceptions comes with two major benefits:
* The duplication of the exceptions declarations and ``++PRAGMA EXCEPTION_INIT++`` is avoided
* The risk of associating multiple different exceptions to the same number is reduced
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2021-04-28 16:49:39 +02:00
----
SET SERVEROUTPUT ON
DECLARE
user_not_found EXCEPTION;
PRAGMA EXCEPTION_INIT(user_not_found, -20000); -- Noncompliant, user_not_found is bound to -20000
BEGIN
NULL;
END;
/
DECLARE
user_not_found EXCEPTION;
PRAGMA EXCEPTION_INIT(user_not_found, -20000); -- Noncompliant, user_not_found is again bound to -20000, duplication
BEGIN
NULL;
END;
/
DECLARE
wrong_password EXCEPTION;
PRAGMA EXCEPTION_INIT(wrong_password, -20000); -- Noncompliant, wrong_password is bound to -20000, conflicting with user_not_found
BEGIN
NULL;
END;
/
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2021-04-28 16:49:39 +02:00
----
SET SERVEROUTPUT ON
CREATE PACKAGE exceptions AS
user_not_found EXCEPTION;
wrong_password EXCEPTION;
PRAGMA EXCEPTION_INIT(user_not_found, -20000); -- Non-Compliant (flag as false-positive)
PRAGMA EXCEPTION_INIT(wrong_password, -20001); -- Non-Compliant (flag as false-positive), conflicts are easier to avoid
END;
/
DROP PACKAGE exceptions;
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Move this exception declaration to a specialized package.
endif::env-github,rspecator-view[]