Giter Club home page Giter Club logo

Comments (2)

PhilippSalvisberg avatar PhilippSalvisberg commented on May 23, 2024

from SonarQube:

"DBMS_UTILITY.FORMAT_ERROR_STACK" and "FORMAT_ERROR_BACKTRACE" should be used together

Since Oracle 10g, DBMS_UTILITY.FORMAT_ERROR_BACKTRACE is available to get an exception's stack trace, i.e. files and lines that lead up to the exception. When combined with DBMS_UTILITY.FORMAT_ERROR_STACK, which contains the exception error code and message, developers are able quickly identify defects.

This rule verifies that whenever either is used in an exception handler, the other is used as well.

Noncompliant Code Example

BEGIN
  RAISE_APPLICATION_ERROR(-20000, 'This is an error example');
EXCEPTION
  WHEN OTHERS THEN  -- Noncompliant; only FORMAT_ERROR_STACK is used
    DBMS_OUTPUT.PUT(DBMS_UTILITY.FORMAT_ERROR_STACK);           -- "ORA-20000: This is an error example"
    DBMS_OUTPUT.PUT_LINE('');
END;
/

Compliant Solution

BEGIN
  RAISE_APPLICATION_ERROR(-20000, 'This is an error example');
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT(DBMS_UTILITY.FORMAT_ERROR_STACK);           -- "ORA-20000: This is an error example"
    DBMS_OUTPUT.PUT(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);       -- "ORA-06512: at line 2"
    DBMS_OUTPUT.PUT_LINE('');
END;
/

from plsql-and-sql-coding-guidelines.

PhilippSalvisberg avatar PhilippSalvisberg commented on May 23, 2024

Sound reasonable. However, consider the following exception block:

   EXCEPTION
      WHEN OTHERS THEN
         -- handle backtrace error stack
         l_backtrace := dbms_utility.format_error_backtrace;
         -- log incident and stop logging
         ...logging.process_stop(
            i_process_id           => l_process_id,
            i_incident_description => SQLERRM || chr(10) || l_backtrace
         );
         -- raise exception after logging
         RAISE;

SQLERRM was used Instead of dbms_utility.format_error_stack in this case. That's ok from my point of view. The sequence of the calls should not matter. Never. It really depends how the logging system works.

Using format_error_backtrace together with SQLERR, SQLERRM or format_error_stack makes sense.

from plsql-and-sql-coding-guidelines.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.