@Test public void testSanitize() { TeiidException te = new TeiidException(JDBCPlugin.Event.TEIID20000, "you don't want to see this"); te.initCause(new Exception("or this")); Throwable t = ExceptionUtil.sanitize(te, true); assertTrue(t.getStackTrace().length != 0); assertNotNull(t.getCause()); assertEquals("TEIID20000", t.getMessage()); assertEquals("java.lang.Exception", t.getCause().getMessage()); t = ExceptionUtil.sanitize(te, false); assertEquals(0, t.getStackTrace().length); assertEquals("TEIID20000", t.getMessage()); }
public static Throwable sanitize(Throwable t, boolean perserveStack) { String code = null; if (t instanceof TeiidException) { code = ((TeiidException)t).getCode(); } else if (t instanceof TeiidRuntimeException) { code = ((TeiidRuntimeException)t).getCode(); } else { code = t.getClass().getName(); } Throwable child = null; if (t.getCause() != null && t.getCause() != t) { child = sanitize(t.getCause(), perserveStack); } Class<?> clazz = t.getClass(); Throwable result = null; while (clazz != null) { if (clazz == Throwable.class || clazz == Exception.class) { break; } Constructor<?> ctor = null; try { ctor = clazz.getDeclaredConstructor(new Class<?>[] {String.class}); result = (Throwable) ctor.newInstance(code); break; } catch (Exception e) { } clazz = clazz.getSuperclass(); } if (result == null) { result = new TeiidException(code); } if (result instanceof TeiidException) { ((TeiidException)result).setCode(code); } else if (result instanceof TeiidRuntimeException) { ((TeiidException)result).setCode(code); } if (child != null) { result.initCause(child); } if (perserveStack) { result.setStackTrace(t.getStackTrace()); } else { result.setStackTrace(SourceWarning.EMPTY_STACK_TRACE); } return result; }
ExceptionUtil { public static Throwable sanitize(Throwable t, boolean perserveStack) { String code = null; if (t instanceof TeiidException) { code = ((TeiidException)t).getCode(); } else if (t instanceof TeiidRuntimeException) { code = ((TeiidRuntimeException)t).getCode(); } else { code = t.getClass().getName(); } Throwable child = null; if (t.getCause() != null && t.getCause() != t) { child = sanitize(t.getCause(), perserveStack); } Class<?> clazz = t.getClass(); Throwable result = null; while (clazz != null) { if (clazz == Throwable.class || clazz == Exception.class) { break; } Constructor<?> ctor = null; try { ctor = clazz.getDeclaredConstructor(new Class<?>[] {String.class}); result = (Throwable) ctor.newInstance(code); break; } catch (Exception e) { } clazz = clazz.getSuperclass(); } if (result == null) { result = new TeiidException(code); } if (result instanceof TeiidException) { ((TeiidException)result).setCode(code); } else if (result instanceof TeiidRuntimeException) { ((TeiidException)result).setCode(code); } if (child != null) { result.initCause(child); } if (perserveStack) { result.setStackTrace(t.getStackTrace()); } else { result.setStackTrace(SourceWarning.EMPTY_STACK_TRACE); } return result; } }
ExceptionUtil { public static Throwable sanitize(Throwable t, boolean perserveStack) { String code = null; if (t instanceof TeiidException) { code = ((TeiidException)t).getCode(); } else if (t instanceof TeiidRuntimeException) { code = ((TeiidRuntimeException)t).getCode(); } else { code = t.getClass().getName(); } Throwable child = null; if (t.getCause() != null && t.getCause() != t) { child = sanitize(t.getCause(), perserveStack); } Class<?> clazz = t.getClass(); Throwable result = null; while (clazz != null) { if (clazz == Throwable.class || clazz == Exception.class) { break; } Constructor<?> ctor = null; try { ctor = clazz.getDeclaredConstructor(new Class<?>[] {String.class}); result = (Throwable) ctor.newInstance(code); break; } catch (Exception e) { } clazz = clazz.getSuperclass(); } if (result == null) { result = new TeiidException(code); } if (result instanceof TeiidException) { ((TeiidException)result).setCode(code); } else if (result instanceof TeiidRuntimeException) { ((TeiidException)result).setCode(code); } if (child != null) { result.initCause(child); } if (perserveStack) { result.setStackTrace(t.getStackTrace()); } else { result.setStackTrace(SourceWarning.EMPTY_STACK_TRACE); } return result; }  }
ExceptionUtil { public static Throwable sanitize(Throwable t, boolean perserveStack) { String code = null; if (t instanceof TeiidException) { code = ((TeiidException)t).getCode(); } else if (t instanceof TeiidRuntimeException) { code = ((TeiidRuntimeException)t).getCode(); } else { code = t.getClass().getName(); } Throwable child = null; if (t.getCause() != null && t.getCause() != t) { child = sanitize(t.getCause(), perserveStack); } Class<?> clazz = t.getClass(); Throwable result = null; while (clazz != null) { if (clazz == Throwable.class || clazz == Exception.class) { break; } Constructor<?> ctor = null; try { ctor = clazz.getDeclaredConstructor(new Class<?>[] {String.class}); result = (Throwable) ctor.newInstance(code); break; } catch (Exception e) { } clazz = clazz.getSuperclass(); } if (result == null) { result = new TeiidException(code); } if (result instanceof TeiidException) { ((TeiidException)result).setCode(code); } else if (result instanceof TeiidRuntimeException) { ((TeiidException)result).setCode(code); } if (child != null) { result.initCause(child); } if (perserveStack) { result.setStackTrace(t.getStackTrace()); } else { result.setStackTrace(SourceWarning.EMPTY_STACK_TRACE); } return result; }  static T getExceptionOfType(Throwable ex, Class<T> cls); static Throwable convertException(Method method, Throwable exception); static Throwable sanitize(Throwable t, boolean perserveStack); }
ExceptionUtil { public static Throwable sanitize(Throwable t, boolean perserveStack) { String code = null; if (t instanceof TeiidException) { code = ((TeiidException)t).getCode(); } else if (t instanceof TeiidRuntimeException) { code = ((TeiidRuntimeException)t).getCode(); } else { code = t.getClass().getName(); } Throwable child = null; if (t.getCause() != null && t.getCause() != t) { child = sanitize(t.getCause(), perserveStack); } Class<?> clazz = t.getClass(); Throwable result = null; while (clazz != null) { if (clazz == Throwable.class || clazz == Exception.class) { break; } Constructor<?> ctor = null; try { ctor = clazz.getDeclaredConstructor(new Class<?>[] {String.class}); result = (Throwable) ctor.newInstance(code); break; } catch (Exception e) { } clazz = clazz.getSuperclass(); } if (result == null) { result = new TeiidException(code); } if (result instanceof TeiidException) { ((TeiidException)result).setCode(code); } else if (result instanceof TeiidRuntimeException) { ((TeiidException)result).setCode(code); } if (child != null) { result.initCause(child); } if (perserveStack) { result.setStackTrace(t.getStackTrace()); } else { result.setStackTrace(SourceWarning.EMPTY_STACK_TRACE); } return result; }  static T getExceptionOfType(Throwable ex, Class<T> cls); static Throwable convertException(Method method, Throwable exception); static Throwable sanitize(Throwable t, boolean perserveStack);  }