Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonar complaining about logging and rethrowing the exception

I have the following piece of code in my program and I am running SonarQube 5 for code quality check on it after integrating it with Maven.

However, Sonar is complaining that I should Either log or rethrow this exception.

What am I missing here? Am I not already logging the exception?

 private boolean authenticate(User user) {
        boolean validUser = false;
        int validUserCount = 0;
        try {
            DataSource dataSource = (DataSource) getServletContext().getAttribute("dataSource");
            validUserCount = new MasterDao(dataSource).getValidUserCount(user);
        } catch (SQLException sqle) {
            LOG.error("Exception while validating user credentials for user with username: " + user.getUsername() + " and pwd:" + user.getPwd());
            LOG.error(sqle.getMessage());
        }
        if (validUserCount == 1) {
            validUser = true;
        }
        return validUser;
    }
like image 314
Nital Avatar asked Jan 24 '15 04:01

Nital


People also ask

Is it possible to log to stdout from SonarQube?

Maybe this is a lightweight utility that will run in a container and does not need a whole logging library just to log to stdout. We should note that it's also possible to mark a violation as a false-positive within the SonarQube user interface.

How to exclude sonar checks in Java?

In Java, we can exclude Sonar checks using the built-in @SuppressWarnings annotation. We can annotate the function: This works exactly the same way as suppressing compiler warnings. All we have to do is specify the rule identifier, in this case java:S106. 4.2.

Why is SonarQube reporting this code as code smell?

Let's look at an example: By default, SonarQube reports this code as a Code Smell due to the java:S106 rule violation: However, let's imagine that for this particular class, we've decided that logging with System.out is valid.

Can I change the ruleset on the SonarQube's server?

While it's possible to change the ruleset on the SonarQube's server, we'll focus only on how to control individual checks within the source code and configuration of our project. 2. Violation Example Let's look at an example: By default, SonarQube reports this code as a Code Smell due to the java:S106 rule violation:


3 Answers

You should do it this way :

try {
    DataSource dataSource = (DataSource) getServletContext().getAttribute("dataSource");
    validUserCount = new MasterDao(dataSource).getValidUserCount(user);
} catch (SQLException sqle) {
    LOG.error("Exception while validating user credentials for user with username: " +
            user.getUsername() + " and pwd:" + user.getPwd(), sqle);
}

Sonar shouldn't bother you anymore

like image 114
abarre Avatar answered Oct 16 '22 14:10

abarre


What sonar is asking you to do, is to persist the entire exception object. You can use something like:

    try {
        ...         
    } catch (Exception e) {
        logger.error("Error", e);
    }
like image 35
Daniele Avatar answered Oct 16 '22 15:10

Daniele


I stumbled across the same issue. I'm not 100% sure if I'm completely right at this point, but basically you should rethrow or log the complete Exception. Whereas e.getMessage() just gives you the detailed message but not the snapshot of the execution stack.

From the Oracle docs (Throwable):

A throwable contains a snapshot of the execution stack of its thread at the time it was created. It can also contain a message string that gives more information about the error. Over time, a throwable can suppress other throwables from being propagated. Finally, the throwable can also contain a cause: another throwable that caused this throwable to be constructed. The recording of this causal information is referred to as the chained exception facility, as the cause can, itself, have a cause, and so on, leading to a "chain" of exceptions, each caused by another.

This means the solution provided by abarre works, because the whole exception object (sqle) is being passed to the logger.

Hope it helps. Cheers.

like image 38
ya man Avatar answered Oct 16 '22 15:10

ya man