-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #873 from csokol/catching-interceptors-validation-…
…errors Handling ValidationExceptions in interceptors
- Loading branch information
Showing
11 changed files
with
292 additions
and
99 deletions.
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.