-
Notifications
You must be signed in to change notification settings - Fork 330
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
Handling ValidationExceptions in interceptors #873
Conversation
|
||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
public class ExceptionHandler { |
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 already have an exception handler. What you think to improve then instead of creating a new one?
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.
this is not the same idea of the other one, I guess...
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.
So a rename is welcome to avoid mistakes.
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'll try to find a better name...
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've renamed to ExecuteMethodExceptionHandler, what do you think @garcia-jj?
I really liked the Try class. For me, it is a nice abstraction and could be On Mon, Nov 10, 2014 at 2:02 PM, csokol [email protected] wrote:
Alberto Souza www.caelum.com.br |
|
||
@Override | ||
public Exception getException() { | ||
throw new UnsupportedOperationException(); |
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.
May be null
is better than this exception, because
-
Success means that have no exception, so return null is right;
-
in the view we can do something like
${not empty try.exception}
. If this exception we need to write more code:${try.failed ? try.exception : null}
;
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 like the query method :) to know about the state of the object.
On Mon, Nov 10, 2014 at 3:47 PM, Otávio Garcia [email protected]
wrote:
In vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java:
- public static class Success extends Try {
private final T result;
public Success(T result) {
this.result = result;
}
@Override
public boolean failed() {
return false;
}
@Override
public Exception getException() {
throw new UnsupportedOperationException();
May be null is better than this exception, because
Success means that have no exception, so return null is right;
in the view we can do something like ${not empty try.exception}. If
this exception we need to write more code: ${try.failed ? try.exception :
null};—
Reply to this email directly or view it on GitHub
https://github.com/caelum/vraptor4/pull/873/files#r20098586.
Alberto Souza
www.caelum.com.br
www.codecrushing.com/products/book-play-framework-java
www.leanpub.com/playframeworknapratica
www.alots.wordpress.com/
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 usually try to avoid passing nulls around. What do you thinkg of returning an empty Optional? (from guava)
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 can't see why too complex. There is no reason to export an 3rd part library in this case. If there is no exception, null
can be welcome.
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 should avoid nulls if we can...
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.
IMHO, as a framework, we should not expose 3rd part library in our public API. Java Optional
is only available in Java 8. So null
are welcome here. We already use null
in other places like converters and more.
Instead of adding a 3rd part library in our public API I prefer this UnsupportedOperationException
so.
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'd also avoid to expose Guava's Optional in our public API.
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'll keep the UnsupportedOperationException, then.
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.
👍
What you think to implement Try.failed()
to return always false
and getException()
and getResult()
to always throws the Exception?
So in the Success
you need to only implements getResult()
, and in the Failure
you need only to override failed()
and getException()
.
The same approaching is used in AbstractList
. All methods are implemented with some common behavior, and subclasses only define a new behaviors as needed.
👍 and it becomes even better for java 8 users. It's a nice addition |
@@ -58,55 +60,50 @@ | |||
|
|||
private Event<MethodExecuted> methodExecutedEvent; | |||
private Event<MethodReady> methodReady; |
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.
please, can you add final for other fields too?
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.
done 👌
after code is done, can you add some docs about it? thks |
Sure, we can merge it now. But we'll need to hold a little more the 4.2 final |
Do you want to merge it right now? I still need to implement the validation exception logic around interceptors. Should I do it in another PR? |
I vote to merge right now, because do not change public API. Just for label: this pull request conflicts with #848, that needs to be refactored after this merge. |
Or due this is a new feature, and we are working in a patch version, we can generate a new version 4.1.2 today or tomorrow, and merge this pull request in 4.2, that is a better place to add new features. But I'm not sure what's the best way. |
I've finished implementing the validation exceptions handling around interceptor calls, using the idea of @lucascs with a new |
@garcia-jj I agree merging it in 4.2, since its a new feature and not a bugfix. |
@csokol, sure! Just let us know when its done to review it again. Thanks |
I've just updated the PR with the code to handle validation exceptions in On Wed Nov 12 2014 at 9:48:32 AM Rodrigo Turini [email protected]
|
I don't remember: why we need this new annotation? |
The whole idea of this PR was to deal with ValidationExceptions inside The idea from @lucascs was to create a new annotation and the users should I don't remember: why we need this new annotation? — |
well, we don't NEED this new annotation, but since we already have some annotations that "generate" code (like |
Exactly :-) Another option is to assume that this will be default behavior from now on |
There is a downside for doing it? (something like performance issues) |
I don't see any performance issue with this wrapping all the interceptors around this try catch. Actually it should be more efficient since we won't need to lookup for the annotation of each interceptor executed. |
And so do I. +1 for keep all interceptors validated by defaul. |
it makes sense... since ExecuteMethod is not an interceptor anymore, I don't see a problem doing this try..catch for every interceptor. |
return e; | ||
} | ||
} | ||
|
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.
Two empty lines here ✂️
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.
👌
@csokol The PR cannot be merged due to conflicts. You need to solve it before merging |
I'll rebase my branch and force a push into this branch On Mon Dec 22 2014 at 2:33:13 PM Nykolas Laurentino de Lima <
|
05625dc
to
9c1ee3e
Compare
/** | ||
* @deprecated CDI eyes only | ||
*/ | ||
protected ExecuteMethod() { |
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 don't need this, ExecuteMethod
has dependent scope
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.
👌
@csokol this PR looks great, thanks a lot! Before merging, can you add a simple |
🎅 |
Just finished the docs, @Turini, can you take a look? I think is ready to |
🚀 |
…errors Handling ValidationExceptions in interceptors
@Turini can we make a relase with it? The |
hey @nykolaslima. sure, I'm planning to do that tomorrow during the morning. Will let u know |
done! |
Is this available on 4.2.0-RC5? I'm getting error 500 on this code: |
Please do not merge it yet, it's a work in progress related with #858
My idea here is to extract classes to better deal with exception handling so then we could reuse the exception handling code to develop that
@ValidationInterceptor
feature that @lucascs proposed.I've implemented a
Try
class, similar to the scala's Try monad. Is not a proper monad here, but I think it's a nice abstraction to handle exceptions, what do you think? @lucascs @Turini @garcia-jj