xueyin87 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Futures exception parser #310

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi.

In jclouds, we have use functions to convert acceptable exceptions into types.  

Function<Exception, T>

For example, an Http 404 exception can be safely converted to false for an 
"exists" function.

I'd like a way to hook into either Futures.compose or Futures.chain and specify 
how, optionally to 
covert the exception to a type.

Here's an example of one of our functions:

@Singleton
public class ReturnFalseOn404 implements Function<Exception, Boolean> {

   public Boolean apply(Exception from) {
      if (from instanceof HttpResponseException) {
         HttpResponseException responseException = (HttpResponseException) from;
         if (responseException.getResponse().getStatusCode() == 404) {
            return false;
         }
      }
      return null;
   }

}

Original issue reported on code.google.com by fernc...@gmail.com on 7 Jan 2010 at 1:39

GoogleCodeExporter commented 9 years ago
the magic here is that null implies the conversion failed and go ahead and 
throw the exception

Original comment by adrian.f...@gmail.com on 7 Jan 2010 at 2:08

GoogleCodeExporter commented 9 years ago
looks like you guys implemented this:

http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/util/c
oncurrent/Futures.html#makeChecked%28java.util.concurrent.Future,%20com.google.c
ommon.base.Function%29

you can close this issue

Original comment by adrian.f...@gmail.com on 28 Jul 2010 at 10:51

GoogleCodeExporter commented 9 years ago
my mistake this isn't the same as makechecked. please keep this open

Original comment by fernc...@gmail.com on 28 Jul 2010 at 11:14

GoogleCodeExporter commented 9 years ago
I'm having a hard time wrapping my head around what exactly this would look 
like. Can you be more specific in your request?

Original comment by fry@google.com on 26 Jan 2011 at 10:34

GoogleCodeExporter commented 9 years ago
sortof like this:

public static <T> ListenableFuture<T> coerceExceptions(ListenableFuture<T> in, 
Function<Exception, T> coercer);

Original comment by fernc...@gmail.com on 26 Jan 2011 at 10:38

GoogleCodeExporter commented 9 years ago
In other words you want all exceptions to either be coerced into valid return 
values, or else thrown as is?

Original comment by fry@google.com on 28 Jan 2011 at 3:50

GoogleCodeExporter commented 9 years ago
Right. Im flexible, but current behavior is to return null instead of throwing 
in the function. This allows you to not have to declare checked exceptions. 

The only tricky part is dealing with Void, as returning a valid Void object 
looks awkward. 

Original comment by adrian.f...@gmail.com on 28 Jan 2011 at 3:58

GoogleCodeExporter commented 9 years ago
Hehe. I ended up needing exactly the same thing.
Except I'd add an executor version (and maybe widen exception type), so:

Throwing exception in the function instead of returning 'null' seems more 
adequate to me, what if my 'null' is a valid value?
What if my function throws a different exception? That has to be handled anyway.

public static <T> ListenableFuture<T> transformFailure(ListenableFuture<T> in, 
Function<Throwable, T> fn);
public static <T> ListenableFuture<T> transformFailure(ListenableFuture<T> in, 
Function<Throwable, T> fn, Executor executor);

Original comment by earwin@gmail.com on 5 Jun 2011 at 1:57

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 13 Jul 2011 at 6:18

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 10 Dec 2011 at 3:42

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 16 Feb 2012 at 7:17

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 30 May 2012 at 7:43

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 22 Jun 2012 at 6:16

GoogleCodeExporter commented 9 years ago
you can close this case, as FutureFallback does the trick

Original comment by fernc...@gmail.com on 27 Dec 2012 at 4:28

GoogleCodeExporter commented 9 years ago
How nice!

Original comment by kevinb@google.com on 27 Dec 2012 at 8:21

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:16

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:10