FaultUtils { public static Fault exceptionToFault(CharSequence entityId, Throwable throwable, Map<CharSequence, CharSequence> auditSupplementaryData) { Fault.Builder faultBuilder = Fault.newBuilder(); faultBuilder.setInputObjectId(entityId); faultBuilder.setTimestamp(System.currentTimeMillis()); faultBuilder.setCode(throwable.getClass().getName()); faultBuilder.setMessage(throwable.getMessage()); StringWriter strWriter = new StringWriter(); PrintWriter pw = new PrintWriter(strWriter); throwable.printStackTrace(pw); pw.close(); faultBuilder.setStackTrace(strWriter.toString()); if (throwable.getCause()!=null) { faultBuilder.setCauses(appendThrowableToCauses( throwable.getCause(), new ArrayList<Cause>())); } if (auditSupplementaryData!=null && !auditSupplementaryData.isEmpty()) { faultBuilder.setSupplementaryData(auditSupplementaryData); } return faultBuilder.build(); } private  FaultUtils(); static Fault exceptionToFault(CharSequence entityId, Throwable throwable,
			Map<CharSequence, CharSequence> auditSupplementaryData);  }
@Test public void testExceptionToFault() throws Exception { String entityId = "id"; Throwable causeThrowable = new InvalidParameterException("cause message"); Throwable rootThrowable = new RuntimeException("root meesage", causeThrowable); String supplementaryDataKey = "auxKey"; String supplementaryDataValue = "auxValue"; Map<CharSequence, CharSequence> auditSupplementaryData = new HashMap<>(); auditSupplementaryData.put(supplementaryDataKey, supplementaryDataValue); Fault fault = FaultUtils.exceptionToFault(entityId, rootThrowable, auditSupplementaryData); assertNotNull(fault); assertEquals(entityId, fault.getInputObjectId()); assertNotNull(fault.getTimestamp()); assertEquals(rootThrowable.getClass().getName(), fault.getCode()); assertEquals(rootThrowable.getMessage(), fault.getMessage()); assertNotNull(fault.getStackTrace()); assertNotNull(fault.getSupplementaryData()); assertEquals(1, fault.getSupplementaryData().size()); assertEquals(supplementaryDataValue, fault.getSupplementaryData().get(supplementaryDataKey)); assertNotNull(fault.getCauses()); assertEquals(1, fault.getCauses().size()); assertEquals(causeThrowable.getClass().getName(), fault.getCauses().get(0).getCode()); assertEquals(causeThrowable.getMessage(), fault.getCauses().get(0).getMessage()); }