Skip to content
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
merged 13 commits into from
Dec 29, 2014

Conversation

csokol
Copy link
Contributor

@csokol csokol commented Nov 10, 2014

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


import static org.slf4j.LoggerFactory.getLogger;

public class ExceptionHandler {
Copy link
Member

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?

Copy link
Member

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...

Copy link
Member

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.

Copy link
Contributor Author

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...

Copy link
Contributor Author

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?

@asouza
Copy link
Contributor

asouza commented Nov 10, 2014

I really liked the Try class. For me, it is a nice abstraction and could be
reused by VRaptor's users in their business logic.

On Mon, Nov 10, 2014 at 2:02 PM, csokol notifications@github.com wrote:

Please do not merge it yet, it's a work in progress related with #858
#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 https://github.com/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 https://github.com/lucascs
@Turini https://github.com/Turini @garcia-jj

https://github.com/garcia-jj

You can merge this Pull Request by running

git pull https://github.com/csokol/vraptor4 catching-interceptors-validation-errors

Or view, comment on, or merge it at:

#873
Commit Summary

  • refactoring exception handling code with a a Try class

File Changes

Patch Links:


Reply to this email directly or view it on GitHub
#873.

Alberto Souza

www.caelum.com.br
www.codecrushing.com/products/book-play-framework-java
www.leanpub.com/playframeworknapratica
www.alots.wordpress.com/


@Override
public Exception getException() {
throw new UnsupportedOperationException();
Copy link
Member

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

  1. Success means that have no exception, so return null is right;

  2. 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};

Copy link
Contributor

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 notifications@github.com
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

  1. Success means that have no exception, so return null is right;

  2. 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/

Copy link
Contributor Author

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)

Copy link
Member

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.

Copy link
Member

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...

Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

@Turini
Copy link
Member

Turini commented Nov 11, 2014

I really liked the Try class. For me, it is a nice abstraction and could
be reused by VRaptor's users in their business logic.

👍 and it becomes even better for java 8 users. It's a nice addition

@Turini Turini added this to the 4.2.0.Final milestone Nov 11, 2014
@@ -58,55 +60,50 @@

private Event<MethodExecuted> methodExecutedEvent;
private Event<MethodReady> methodReady;
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 👌

@Turini
Copy link
Member

Turini commented Nov 11, 2014

after code is done, can you add some docs about it? thks

@garcia-jj
Copy link
Member

I like this pull request. Code are too pretty and as @Turini said, its a nice addition the Try class. Thank you.

@Turini Whats the target release? Since this not change our public API can be merged in the current release, right? What you think?

@Turini
Copy link
Member

Turini commented Nov 11, 2014

Since this not change our public API can be merged in the current release,
right? What you think?

Sure, we can merge it now. But we'll need to hold a little more the 4.2 final
to ensure its working as expected (if possible deploy it on mamute and other apps)

@csokol
Copy link
Contributor Author

csokol commented Nov 11, 2014

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?

@garcia-jj
Copy link
Member

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.

@garcia-jj
Copy link
Member

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.

@csokol
Copy link
Contributor Author

csokol commented Nov 11, 2014

I've finished implementing the validation exceptions handling around interceptor calls, using the idea of @lucascs with a new @ValidatedInterceptor annotation. Can I push this update in this PR?

@Turini
Copy link
Member

Turini commented Nov 12, 2014

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.

@garcia-jj I agree merging it in 4.2, since its a new feature and not a bugfix.
About the release, we need to fix the current bugs (i) before doing it. I'll try to
work on it as soon as possible. (i) #876 and #832. The #867 was just merged

@Turini
Copy link
Member

Turini commented Nov 12, 2014

I've finished implementing the validation exceptions handling around
interceptor calls [...] Can I push this update in this PR?

@csokol, sure! Just let us know when its done to review it again. Thanks

@csokol
Copy link
Contributor Author

csokol commented Nov 12, 2014

I've just updated the PR with the code to handle validation exceptions in
interceptors annotated with @ValidatedInterceptor, can you guys review it?

On Wed Nov 12 2014 at 9:48:32 AM Rodrigo Turini notifications@github.com
wrote:

I've finished implementing the validation exceptions handling around
interceptor calls [...] Can I push this update in this PR?

@csokol https://github.com/csokol, sure! Just let us know when its done
to review it again. Thanks


Reply to this email directly or view it on GitHub
#873 (comment).

@garcia-jj
Copy link
Member

I don't remember: why we need this new annotation?

@csokol
Copy link
Contributor Author

csokol commented Nov 12, 2014

The whole idea of this PR was to deal with ValidationExceptions inside
interceptors. Currently, if an interceptor does something like:
validator.onErrorRedirect(..). The exception comes up and the request end
with a 500.

The idea from @lucascs was to create a new annotation and the users should
use it to define what interceptors should be treated differently to support
validation errors.

I don't remember: why we need this new annotation?


Reply to this email directly or view it on GitHub
#873 (comment).

@lucascs
Copy link
Member

lucascs commented Nov 13, 2014

well, we don't NEED this new annotation, but since we already have some annotations that "generate" code (like @AcceptsWithAnnotation), @ValidationInterceptor would "generate" the try...catch(ValidationException) around the interceptor.

@csokol
Copy link
Contributor Author

csokol commented Nov 13, 2014

Exactly :-)

Another option is to assume that this will be default behavior from now on
and remove the annotation.

@Turini
Copy link
Member

Turini commented Nov 24, 2014

Another option is to assume that this will be default behavior from
now on and remove the annotation.

There is a downside for doing it? (something like performance issues)

@csokol
Copy link
Contributor Author

csokol commented Nov 26, 2014

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.

@Turini
Copy link
Member

Turini commented Nov 26, 2014

And so do I. +1 for keep all interceptors validated by defaul.

@lucascs
Copy link
Member

lucascs commented Nov 28, 2014

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;
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two empty lines here ✂️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

@nykolaslima
Copy link
Contributor

@csokol The PR cannot be merged due to conflicts. You need to solve it before merging

@csokol
Copy link
Contributor Author

csokol commented Dec 22, 2014

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 <
notifications@github.com> wrote:

@csokol https://github.com/csokol The PR cannot be merged due to
conflicts. You need to solve it before merging


Reply to this email directly or view it on GitHub
#873 (comment).

@csokol csokol force-pushed the catching-interceptors-validation-errors branch from 05625dc to 9c1ee3e Compare December 22, 2014 17:10
/**
* @deprecated CDI eyes only
*/
protected ExecuteMethod() {
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

@Turini
Copy link
Member

Turini commented Dec 23, 2014

@csokol this PR looks great, thanks a lot! Before merging, can you add a simple
paragraph on vraptor-site about this feature? And if possible javadoc Try class

@lucascs
Copy link
Member

lucascs commented Dec 23, 2014

🎅

@csokol
Copy link
Contributor Author

csokol commented Dec 29, 2014

Just finished the docs, @Turini, can you take a look? I think is ready to :shipit:

@Turini
Copy link
Member

Turini commented Dec 29, 2014

🚀

csokol added a commit that referenced this pull request Dec 29, 2014
…errors

Handling ValidationExceptions in interceptors
@csokol csokol merged commit 3268b17 into caelum:master Dec 29, 2014
@csokol csokol deleted the catching-interceptors-validation-errors branch December 29, 2014 15:31
@nykolaslima
Copy link
Contributor

@Turini can we make a relase with it? The ValidationException thrown from an interceptor is fixed by this PR but the last VRaptor release (4.2.0-RC3) doesn't have this fix.

@Turini
Copy link
Member

Turini commented May 5, 2016

hey @nykolaslima.

sure, I'm planning to do that tomorrow during the morning. Will let u know

@Turini
Copy link
Member

Turini commented May 9, 2016

done!

@filipesg
Copy link

filipesg commented May 24, 2017

Is this available on 4.2.0-RC5? I'm getting error 500 on this code:
validator.onErrorForwardTo("/url");
java.lang.RuntimeException: java.lang.String is final
It's inside an interceptor. I need to use any kind of extra annotation to catch this validation error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants