¿Cómo evito la infracción de "preservar el seguimiento de la pila" de Sonar?
Frecuentes
Visto 4,372 equipos
1
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) {
LOGGER.info(e.getMessage());
} catch (JsonMappingException e) {
LOGGER.info(e.getMessage());
} catch (IOException e) {
LOGGER.info(e.getMessage());
}
throw new PhDashException(pHDashExceptionMapper.getMessage());
}
1 Respuestas
2
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 java or haz tu propia pregunta.
Try passing the whole exception to the logger instead of its message. - Sotirios Delimanolis
@Niks Tyagi - But the best practice is to Avoid printStackTrace()?right? gazelle.ihe.net/sonar/rules/show/… - Amila Iddamalgoda
yes update as LOGGER.info(exception) - Kick