google-code-export / objectify-appengine

Automatically exported from code.google.com/p/objectify-appengine
MIT License
1 stars 0 forks source link

Cannot throw ServiceException within objectify Transaction #218

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Normally, I would throw a ServiceException to tell App Engine to return an http 
error and stop processing.

    @ApiMethod(
            name = "registrations.register",
            httpMethod = ApiMethod.HttpMethod.POST,
            path = "registrations/register"
    )
    public Registration registerUser(
            final Registration registration
    ) throws ServiceException {
        //...
    }

During a transaction, I cannot do that, because all App Engine exceptions 
derive from ServiceException, which is checked.

Is there a workaround or would "throws ServiceException" be required in Work<R>?

Currently I see no way to raise a ServiceException from within a transaction, 
except for working around the issue using local variables and ending the 
transaction first, which causes a mess when using sub-routines which also throw 
ServiceException.

Original issue reported on code.google.com by casinit...@gmail.com on 16 Aug 2014 at 4:11

GoogleCodeExporter commented 9 years ago
This applies not just to the Work interface, but to every other standard 
interface in Java (List, Runnable, etc). It's a shame that Google chose to use 
a checked exception for this. Checked exceptions are the worst part of Java.

There's no way to add "throws ServiceException" to Work without irritating 
every other Objectify user (including me). However, there is a workaround. You 
will need to wrap and unwrap the exception across the Work.run() boundary using 
a runtime exception.  Create your own implementation of transact() which 
declares 'throws ServiceException' and takes a YourWork interface (which also 
declares the exception). You can call Objectify's transact() with an 
implementation that wraps and unwraps any ServiceExceptions that are thrown.

I'm closing this issue since it's not a bug; if you would like further help 
please post to the Google Group.

Original comment by lhori...@gmail.com on 27 Aug 2014 at 6:12