Skip to content

Commit

Permalink
[#1518] Propagate exceptions in jobs to promises from .now() and .in(…
Browse files Browse the repository at this point in the history
…) for Controller.await()
  • Loading branch information
shelajev authored and Notalifeform committed Jul 4, 2013
1 parent efbd096 commit d869942
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 8 deletions.
35 changes: 27 additions & 8 deletions framework/src/play/jobs/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import play.Play;
import play.exceptions.JavaExecutionException;
import play.exceptions.PlayException;
import play.exceptions.UnexpectedException;
import play.libs.F.Promise;
import play.libs.Time;

Expand Down Expand Up @@ -63,11 +64,16 @@ public Promise<V> now() {
final Promise<V> smartFuture = new Promise<V>();
JobsPlugin.executor.submit(new Callable<V>() {
public V call() throws Exception {
V result = Job.this.call();
smartFuture.invoke(result);
return result;
try {
V result = Job.this.call();
smartFuture.invoke(result);
return result;
}
catch(Exception e) {
smartFuture.invokeWithException(e);
return null;
}
}

});

return smartFuture;
Expand All @@ -91,11 +97,16 @@ public Promise<V> in(int seconds) {
JobsPlugin.executor.schedule(new Callable<V>() {

public V call() throws Exception {
V result = Job.this.call();
smartFuture.invoke(result);
return result;
try {
V result = Job.this.call();
smartFuture.invoke(result);
return result;
}
catch(Exception e) {
smartFuture.invokeWithException(e);
return null;
}
}

}, seconds, TimeUnit.SECONDS);

return smartFuture;
Expand Down Expand Up @@ -124,9 +135,17 @@ public void onException(Throwable e) {
super.onException(e);
} catch(Throwable ex) {
Logger.error(ex, "Error during job execution (%s)", this);
throw new UnexpectedException(unwrap(e));
}
}

private Throwable unwrap(Throwable e) {
while((e instanceof UnexpectedException || e instanceof PlayException) && e.getCause() != null) {
e = e.getCause();
}
return e;
}

@Override
public void run() {
call();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,54 @@ public static void paramsLocalVariableTracerAndAwait(int a) {
render(a,aa);

}
public static void useAwaitOnFailingJobsPromise(String a) {
Job job = new Job() {
@Override
public void doJob() throws Exception {
throw new RuntimeException("Hello world!");
}
};

Promise promise;
if("now".equals(a)) {
promise = job.now();
}
else {
promise = job.in(1);
}

try {
await(promise);
renderText("ok");
}
catch(Exception e) {
renderText("caught exception: " + e);
}
}
public static void useAwaitOnNormalJobsPromise(String a) {
Job job = new Job() {
@Override
public void doJob() throws Exception {
Logger.trace("Everything is fine, I'm just doing my job!");
}
};

Promise promise;
if("now".equals(a)) {
promise = job.now();
}
else {
promise = job.in(1);
}

try {
await(promise);
renderText("ok");
}
catch(Exception e) {
renderText("caught exception");
}
}


}
Expand Down
12 changes: 12 additions & 0 deletions samples-and-tests/just-test-cases/test/continuations.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@

openAndWait('@{WithContinuations.useAwaitInWebSocketControllerWithoutContinuations()}')
assertTextPresent('failCount: 1')

openAndWait('@{WithContinuations.useAwaitOnFailingJobsPromise("now")}')
assertTextPresent('caught exception')

openAndWait('@{WithContinuations.useAwaitOnFailingJobsPromise("in")}')
assertTextPresent('caught exception')

openAndWait('@{WithContinuations.useAwaitOnNormalJobsPromise("now")}')
assertTextPresent('ok')

openAndWait('@{WithContinuations.useAwaitOnNormalJobsPromise("in")}')
assertTextPresent('ok')

openAndWait('@{WithContinuations.usingRenderArgsAndAwait()}')
assertBodyText('true')
Expand Down

0 comments on commit d869942

Please sign in to comment.