-
Notifications
You must be signed in to change notification settings - Fork 332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handling ValidationExceptions in interceptors #873
Merged
csokol
merged 13 commits into
caelum:master
from
csokol:catching-interceptors-validation-errors
Dec 29, 2014
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
76f67ae
refactoring exception handling code with a a Try class
csokol f7cd903
Try should be generic
csokol 4b17d6c
renaming method and adding exception message
csokol b64e749
renaming class
csokol eadf45d
handling validation exception around interceptors
csokol 646fc1f
removing empty lines
csokol 3603996
catching validation errors for all interceptors and removing annotation
csokol 9c1ee3e
fixing import
csokol ad4b568
removing pointless constructor
csokol 37d89fd
moving try to core
csokol 7d8b1f5
Try javadoc
csokol 59d03b5
fixing imports
csokol 9503b57
Try docs on vraptor-site
csokol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
86 changes: 86 additions & 0 deletions
86
vraptor-core/src/main/java/br/com/caelum/vraptor/core/Try.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package br.com.caelum.vraptor.core; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
/** | ||
* A class to wrap code that can possibly throw exceptions. | ||
* | ||
* Use the static method Try#run to instantiate this class, passing down | ||
* the dangerous code and use its methods to retrieve the result or the exception | ||
* of your computation. Example using java 8: | ||
* | ||
* <code> | ||
* Try try = Try.run(() -> someDangerousMethod()); | ||
* if (try.failed()) { | ||
* Exception e = try.getException(); | ||
* handleError(e); | ||
* } | ||
* try.result(); //do something with the result | ||
* </code> | ||
* | ||
* @author Chico Sokol | ||
* @param <T> the type of the result of your computation | ||
*/ | ||
public abstract class Try<T> { | ||
public static <T> Try run(Callable<T> callable) { | ||
try { | ||
T call = callable.call(); | ||
return new Success(call); | ||
} catch (Exception e) { | ||
return new Failed(e); | ||
} | ||
} | ||
|
||
public abstract boolean failed(); | ||
|
||
public abstract T result(); | ||
|
||
public abstract Exception getException(); | ||
|
||
public static class Success<T> extends Try { | ||
private final T result; | ||
|
||
private Success(T result) { | ||
this.result = result; | ||
} | ||
|
||
@Override | ||
public boolean failed() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Object result() { | ||
return result; | ||
} | ||
|
||
@Override | ||
public Exception getException() { | ||
throw new UnsupportedOperationException("A Success doesn't have an exception."); | ||
} | ||
} | ||
|
||
public static class Failed<T> extends Try { | ||
private final Exception e; | ||
|
||
private Failed(Exception e) { | ||
this.e = e; | ||
} | ||
|
||
@Override | ||
public boolean failed() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Object result() { | ||
throw new UnsupportedOperationException("A Failed doesn't have a result."); | ||
} | ||
|
||
@Override | ||
public Exception getException() { | ||
return e; | ||
} | ||
} | ||
|
||
} |
This file contains 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
39 changes: 39 additions & 0 deletions
39
vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethodExceptionHandler.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package br.com.caelum.vraptor.observer; | ||
|
||
import br.com.caelum.vraptor.InterceptionException; | ||
import br.com.caelum.vraptor.core.ReflectionProviderException; | ||
import br.com.caelum.vraptor.interceptor.ApplicationLogicException; | ||
import br.com.caelum.vraptor.validator.ValidationException; | ||
import org.slf4j.Logger; | ||
|
||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
/** | ||
* Handles exceptions thrown by a controller method | ||
* | ||
* @author Chico Sokol | ||
*/ | ||
public class ExecuteMethodExceptionHandler { | ||
private final static Logger log = getLogger(ExecuteMethodExceptionHandler.class); | ||
|
||
public void handle(Exception exception) { | ||
if (exception instanceof IllegalArgumentException) { | ||
throw new InterceptionException(exception); | ||
} | ||
if (exception instanceof ReflectionProviderException) { | ||
throwIfNotValidationException(exception, exception.getCause()); | ||
} | ||
throwIfNotValidationException(exception, exception); | ||
} | ||
|
||
private void throwIfNotValidationException(Throwable original, Throwable alternativeCause) { | ||
Throwable cause = original.getCause(); | ||
|
||
if (original instanceof ValidationException || cause instanceof ValidationException) { | ||
// fine... already parsed | ||
log.trace("swallowing {}", cause); | ||
} else { | ||
throw new ApplicationLogicException(alternativeCause); | ||
} | ||
} | ||
} |
This file contains 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We always need the
if
, right? maybe we could add ahandleOnFail
method (or something like that) to avoid duplicating... what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm calling the method
onError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
onError looks much better 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a question here, should I create a new interface to use as parameter? something like java 8's Consumer? Or I could use guava Function and use something like
Function<? extends Exception, Void>
. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we decided to drop this idea. In the future, when we start using java 8 we can improve this api.