@Test public void testWaitForTaskWrapsAggregateExceptionAsParseException() { final Exception error = new RuntimeException("This task failed."); final ArrayList<Task<Void>> tasks = new ArrayList<>(); for (int i = 0; i < 20; i++) { final int number = i; Task<Void> task = Task.callInBackground(new Callable<Void>() { @Override public Void call() throws Exception { Thread.sleep((long) (Math.random() * 100)); if (number == 10 || number == 11) { throw error; } return null; } }); tasks.add(task); } try { ParseTaskUtils.wait(Task.whenAll(tasks)); } catch (ParseException e) { assertTrue(e.getCause() instanceof AggregateException); } }
static <T> T wait(Task<T> task) throws ParseException { try { task.waitForCompletion(); if (task.isFaulted()) { Exception error = task.getError(); if (error instanceof ParseException) { throw (ParseException) error; } if (error instanceof AggregateException) { throw new ParseException(error); } if (error instanceof RuntimeException) { throw (RuntimeException) error; } throw new RuntimeException(error); } else if (task.isCancelled()) { throw new RuntimeException(new CancellationException()); } return task.getResult(); } catch (InterruptedException e) { throw new RuntimeException(e); } }
ParseTaskUtils { static <T> T wait(Task<T> task) throws ParseException { try { task.waitForCompletion(); if (task.isFaulted()) { Exception error = task.getError(); if (error instanceof ParseException) { throw (ParseException) error; } if (error instanceof AggregateException) { throw new ParseException(error); } if (error instanceof RuntimeException) { throw (RuntimeException) error; } throw new RuntimeException(error); } else if (task.isCancelled()) { throw new RuntimeException(new CancellationException()); } return task.getResult(); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
ParseTaskUtils { static <T> T wait(Task<T> task) throws ParseException { try { task.waitForCompletion(); if (task.isFaulted()) { Exception error = task.getError(); if (error instanceof ParseException) { throw (ParseException) error; } if (error instanceof AggregateException) { throw new ParseException(error); } if (error instanceof RuntimeException) { throw (RuntimeException) error; } throw new RuntimeException(error); } else if (task.isCancelled()) { throw new RuntimeException(new CancellationException()); } return task.getResult(); } catch (InterruptedException e) { throw new RuntimeException(e); } }  }
ParseTaskUtils { static <T> T wait(Task<T> task) throws ParseException { try { task.waitForCompletion(); if (task.isFaulted()) { Exception error = task.getError(); if (error instanceof ParseException) { throw (ParseException) error; } if (error instanceof AggregateException) { throw new ParseException(error); } if (error instanceof RuntimeException) { throw (RuntimeException) error; } throw new RuntimeException(error); } else if (task.isCancelled()) { throw new RuntimeException(new CancellationException()); } return task.getResult(); } catch (InterruptedException e) { throw new RuntimeException(e); } }   }
ParseTaskUtils { static <T> T wait(Task<T> task) throws ParseException { try { task.waitForCompletion(); if (task.isFaulted()) { Exception error = task.getError(); if (error instanceof ParseException) { throw (ParseException) error; } if (error instanceof AggregateException) { throw new ParseException(error); } if (error instanceof RuntimeException) { throw (RuntimeException) error; } throw new RuntimeException(error); } else if (task.isCancelled()) { throw new RuntimeException(new CancellationException()); } return task.getResult(); } catch (InterruptedException e) { throw new RuntimeException(e); } }    }