public static RuntimeException wrapIf(boolean condition, Throwable t) { if (condition) { return wrap(t); } if (t instanceof RuntimeException) { return (RuntimeException) t; } return new RuntimeException(t); }
private RuntimeException wrapUserCodeException(Throwable t) { throw UserCodeException.wrapIf(!isSystemDoFn(), t); }
public static UserCodeException wrap(Throwable t) { if (t instanceof UserCodeException) { return (UserCodeException) t; } return new UserCodeException(t); }
/** * Truncates the @{Throwable}'s stack trace to contain only user code, removing all frames below. * * <p>This is to remove infrastructure noise below user code entry point. We do this by finding * common stack frames between the throwable's captured stack and that of the current thread. */ private void truncateStackTrace(Throwable t) { StackTraceElement[] currentStack = Thread.currentThread().getStackTrace(); StackTraceElement[] throwableStack = t.getStackTrace(); int currentStackSize = currentStack.length; int throwableStackSize = throwableStack.length; if (throwableStackSize == 0) { // Nothing to truncate. return; } int commonFrames = 0; while (framesEqual( currentStack[currentStackSize - commonFrames - 1], throwableStack[throwableStackSize - commonFrames - 1])) { commonFrames++; if (commonFrames >= Math.min(currentStackSize, throwableStackSize)) { break; } } StackTraceElement[] truncatedStack = Arrays.copyOfRange(throwableStack, 0, throwableStackSize - commonFrames); t.setStackTrace(truncatedStack); }
@Override public boolean matches(Object item) { if (!(item instanceof UserCodeException)) { return false; } UserCodeException that = (UserCodeException) item; return causeMatcher.matches(that.getCause()); }
private UserCodeException wrap(Throwable t) { throw UserCodeException.wrap(t); }
@Test public void testWrapIfOnlyWrapsWhenTrue() { IOException cause = new IOException(); RuntimeException wrapped = UserCodeException.wrapIf(true, cause); assertThat(wrapped, is(instanceOf(UserCodeException.class))); }
private static RuntimeException beamExceptionFrom(final Throwable e) { // Scala doesn't declare checked exceptions in the bytecode, and the Java compiler // won't let you catch something that is not declared, so we can't catch // SparkException directly, instead we do an instanceof check. if (e instanceof SparkException) { if (e.getCause() != null && e.getCause() instanceof UserCodeException) { UserCodeException userException = (UserCodeException) e.getCause(); return new Pipeline.PipelineExecutionException(userException.getCause()); } else if (e.getCause() != null) { return new Pipeline.PipelineExecutionException(e.getCause()); } } return runtimeExceptionFrom(e); }
public SerializableArrayViaCoder(Coder<T> elementCoder, T[] value) { this.coder = ListCoder.of(elementCoder); this.value = value; try { this.encodedValue = CoderUtils.encodeToByteArray(coder, Arrays.asList(value)); } catch (CoderException exc) { throw UserCodeException.wrap(exc); } }
@Test public void testWrapIfReturnsRuntimeExceptionWhenFalse() { IOException cause = new IOException(); RuntimeException wrapped = UserCodeException.wrapIf(false, cause); assertThat(wrapped, is(not(instanceOf(UserCodeException.class)))); assertEquals(cause, wrapped.getCause()); }
@Override public PipelineResult run(Pipeline pipeline) { try { return delegate.run(pipeline); } catch (Throwable t) { // Special case hack to pull out assertion errors from PAssert; instead there should // probably be a better story along the lines of UserCodeException. UserCodeException innermostUserCodeException = null; Throwable current = t; for (; current.getCause() != null; current = current.getCause()) { if (current instanceof UserCodeException) { innermostUserCodeException = (UserCodeException) current; } } if (innermostUserCodeException != null) { current = innermostUserCodeException.getCause(); } if (current instanceof AssertionError) { throw (AssertionError) current; } throw new PipelineExecutionException(current); } }
@Override public PipelineResult run(Pipeline pipeline) { Throwable t = new IllegalStateException("user code exception"); throw UserCodeException.wrap(t); } }
@Test public void testWrapIfReturnsSourceRuntimeExceptionWhenFalse() { RuntimeException runtimeException = new RuntimeException("oh noes!"); RuntimeException wrapped = UserCodeException.wrapIf(false, runtimeException); assertEquals(runtimeException, wrapped); }
@Override public PipelineResult run(Pipeline pipeline) { try { return delegate.run(pipeline); } catch (Throwable t) { // Special case hack to pull out assertion errors from PAssert; instead there should // probably be a better story along the lines of UserCodeException. UserCodeException innermostUserCodeException = null; Throwable current = t; for (; current.getCause() != null; current = current.getCause()) { if (current instanceof UserCodeException) { innermostUserCodeException = ((UserCodeException) current); } } if (innermostUserCodeException != null) { current = innermostUserCodeException.getCause(); } if (current instanceof AssertionError) { throw (AssertionError) current; } throw new PipelineExecutionException(current); } }
@Override public void beforeElement(WindowedValue<T> element) { try { mutationElements.put( element, MutationDetectors.forValueWithCoder(element.getValue(), coder)); } catch (CoderException e) { throw UserCodeException.wrap(e); } }
@Override public PipelineResult run(Pipeline pipeline) { try { return delegate.run(pipeline); } catch (Throwable t) { // Special case hack to pull out assertion errors from PAssert; instead there should // probably be a better story along the lines of UserCodeException. UserCodeException innermostUserCodeException = null; Throwable current = t; for (; current.getCause() != null; current = current.getCause()) { if (current instanceof UserCodeException) { innermostUserCodeException = (UserCodeException) current; } } if (innermostUserCodeException != null) { current = innermostUserCodeException.getCause(); } if (current instanceof AssertionError) { throw (AssertionError) current; } throw new PipelineExecutionException(current); } }
@Override public void processElement(WindowedValue<InputT> element) { try { Iterable<WindowedValue<InputT>> unprocessed = fnRunner.processElementInReadyWindows(element); unprocessedElements.addAll(unprocessed); } catch (Exception e) { throw UserCodeException.wrap(e); } }
throw new Pipeline.PipelineExecutionException(uce.getCause()); } catch (Exception e) { if (e instanceof InterruptedException) {
private ParDoEvaluator( PushbackSideInputDoFnRunner<InputT, ?> fnRunner, AppliedPTransform<?, ?, ?> transform, BundleOutputManager outputManager, DirectStepContext stepContext) { this.fnRunner = fnRunner; this.transform = transform; this.outputManager = outputManager; this.stepContext = stepContext; this.unprocessedElements = ImmutableList.builder(); try { fnRunner.startBundle(); } catch (Exception e) { throw UserCodeException.wrap(e); } }