3

We have SonarQube setup for our org to scan for code quality. One code issue I get when using AuraHandlerException is -

When handling a caught exception, the original exception's message and stack trace should be logged or passed forward to preserve the stack trace.

Example : This is a good exception handling:

good(){

try{
// some code
}Catch(Exception e){
throw new Exception(e);
}

}

This is a bad one according to the code scanner:

bad(){

try{
// some code
}Catch(Exception e){
throw new Exception(e.getmessage());
}

}

Now when I use AuraHandlerException , I am unable to use throw AuraHandlerException(e) - error says no constructor present. SO I have to use throw AuraHandlerException(e.getMessage()). Is there an approach to get pass the code scanner rule for exception handling in this case?

any suggestions?

  • Is there a reason that you are catching the exception just to throw it again? Is there more code that you excluded for clarity? – Ryan Rudd Jan 25 '18 at 15:32
  • Nope, this is what I have. Just throw the exception from the apex controller class. Do you have any concern of throwing an exception in this way? May be it should be handled by storing it in a log file? But this exception is thrown in the aura component. So I guess it will be visible in the user interface! – SfdcBat Jan 26 '18 at 23:51
  • My only comment would be that catching the exception only to throw it again (without doing anything like logging) doesn't do anything differently than just not catching it at all, which I believe in this case would get rid of your issue altogether. – Ryan Rudd Jan 29 '18 at 14:24
1

I would say its just a false positive from the Scanner .Throwing AuraHandled exception using the code like below is completely valid

   throw new AuraHandledException(e.getMessage());//Valid 
  • I guess these code scan rules are created based on Java. – SfdcBat Jan 26 '18 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.