rspec/rules/S2221/plsql/rule.adoc

99 lines
1.7 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2020-06-30 12:48:07 +02:00
Before trapping all possible exceptions, it is best to try to trap the specific ones and try to recover from those.
=== Noncompliant code example
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2020-06-30 12:48:07 +02:00
----
SET SERVEROUTPUT ON
CREATE TABLE hitCounter
(
page VARCHAR2(42),
hits NUMBER,
CONSTRAINT pk PRIMARY KEY (page)
);
CREATE PROCEDURE hitPage(pageIn VARCHAR2) AS
BEGIN
INSERT INTO hitCounter VALUES (pageIn, 1);
EXCEPTION -- Noncompliant, the only exception handler is WHEN OTHERS
WHEN OTHERS THEN
IF SQLCODE = -1 THEN
UPDATE hitCounter SET hits = hits + 1 WHERE page = pageIn;
ELSE
DBMS_OUTPUT.PUT_LINE('An unknown error occured!');
END IF;
END;
/
BEGIN
hitPage('index.html');
hitPage('index.html');
END;
/
SELECT * FROM hitCounter;
DROP PROCEDURE hitPage;
DROP TABLE hitCounter;
----
=== Compliant solution
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2020-06-30 12:48:07 +02:00
----
SET SERVEROUTPUT ON
CREATE TABLE hitCounter
(
page VARCHAR2(42),
hits NUMBER,
CONSTRAINT pk PRIMARY KEY (page)
);
CREATE PROCEDURE hitPage(pageIn VARCHAR2) AS
BEGIN
INSERT INTO hitCounter VALUES (pageIn, 1);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
UPDATE hitCounter SET hits = hits + 1 WHERE page = pageIn;
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unknown error occured!');
END;
/
BEGIN
hitPage('index.html');
hitPage('index.html');
END;
/
SELECT * FROM hitCounter;
DROP PROCEDURE hitPage;
DROP TABLE hitCounter;
----
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Add exception handlers for the expected exception types.
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]