¿Cómo evito la infracción de "preservar el seguimiento de la pila" de Sonar?

Sonar gives a major violation error ("Preserve Stack Trace") for the following code. Following method is used to throw an exception. What are the steps should I take to overcome this violation?

public void exceptionHandler(String exception) throws PhDashException {

    String exceptionMsg = exception.replaceAll("-", "_");

    ExceptionPhDash pHDashExceptionMapper = new ExceptionPhDash();
    try {
        pHDashExceptionMapper = new ObjectMapper().readValue(exceptionMsg, ExceptionPhDash.class);
    } catch (JsonParseException e) {
    } catch (JsonMappingException e) {
    } catch (IOException e) {
    throw new PhDashException(pHDashExceptionMapper.getMessage());

preguntado el 12 de febrero de 14 a las 05:02

Try passing the whole exception to the logger instead of its message. -

@Niks Tyagi - But the best practice is to Avoid printStackTrace()?right? gazelle.ihe.net/sonar/rules/show/… -

yes update as LOGGER.info(exception) -

1 Respuestas

You've logged each exception; that should be enough as far as preserving information is concerned like below,

LOGGER.info("Unexpected Exception has occurred", e);     

Or You should re throw the PhDashException if you can, because, you should be enough as far as preserving information is concerned

throw new PhDashException(pHDashExceptionMapper);

Respondido el 22 de Septiembre de 15 a las 10:09

But PhDashException use a string parameter.And pHDashExceptionMapper is a customized exception class. public class PhDashException extends Exception{ /** * Serial Id. */ private static final long serialVersionUID = -4859242009502871357L; public PhDashException(String errorMsg) { super(errorMsg); } } - Amila Iddamalgoda

@AmilaIddamalgoda : So, log it properly - Abimarán Kugathasan

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.