Created
February 11, 2014 02:20
-
-
Save richdougherty/8928159 to your computer and use it in GitHub Desktop.
Show non-500 Results for Exceptions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| ) | |
| ); | |
| } | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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