Skip to content

Instantly share code, notes, and snippets.

@richdougherty
Created February 11, 2014 02:20
Show Gist options
  • Select an option

  • Save richdougherty/8928159 to your computer and use it in GitHub Desktop.

Select an option

Save richdougherty/8928159 to your computer and use it in GitHub Desktop.
Show non-500 Results for Exceptions
import actions.*;
...
@Transactional
@ResultExceptionHandling
public static Result list(int page, String sortBy, String order, String filter) {
if (page == 2) throw new ResultException(ok("transaction rolled back"));
return ok(
list.render(
Computer.page(page, 10, sortBy, order, filter),
sortBy, order, filter
)
);
}
...
package actions;
import play.mvc.*;
import play.mvc.Http.*;
public class ResultException extends RuntimeException {
private Result result;
public ResultException(Result result) {
this.result = result;
}
public Result getResult() {
return result;
}
}
package actions;
import play.mvc.*;
import play.mvc.Http.*;
import java.util.*;
import java.lang.annotation.*;
@With(ResultExceptionHandlingAction.class)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ResultExceptionHandling {
String value() default "default";
boolean readOnly() default false;
}
package actions;
import play.libs.F;
import play.mvc.*;
import play.mvc.Http.*;
public class ResultExceptionHandlingAction extends Action<ResultExceptionHandling> {
public Result call(final Context ctx) throws Throwable {
try {
return delegate.call(ctx);
} catch (ResultException resultException) {
return resultException.getResult();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment