How to deal with exceptions in a Rest API using Restfulie?

30 views
Skip to first unread message

RafaelViana

unread,
Feb 8, 2011, 8:12:47 AM2/8/11
to restfulie-java
Considered I want to throws an error for those are using the API.

@Get
@Path("/items/{id}")
public void show(int id)
{
result.use(status()).badRequest("Bad request....");
}

How I get the request status code it in the client?

Guilherme Silveira

unread,
Feb 8, 2011, 8:17:31 AM2/8/11
to restful...@googlegroups.com
Hi Rafael,

In the client you can do:

Response r = Restfulie.at(uri).get();
System.out.println(r.getCode());

Regards

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

RafaelViana

unread,
Feb 8, 2011, 8:44:51 AM2/8/11
to restfulie-java
Hi,

Sorry, I didn't see this method. It's so simple :D

On 8 fev, 11:17, Guilherme Silveira <guilherme.silve...@caelum.com.br>
wrote:
> Hi Rafael,
>
> In the client you can do:
>
> Response r = Restfulie.at(uri).get();
> System.out.println(r.getCode());
>
> Regards
>
> Guilherme Silveira
> Caelum | Ensino e Inovaçãohttp://www.caelum.com.br/

Guilherme Silveira

unread,
Feb 8, 2011, 8:53:00 AM2/8/11
to restful...@googlegroups.com
No problem... feel free to ask other questions

Regards
Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

Jose Donizetti

unread,
Feb 8, 2011, 8:37:15 AM2/8/11
to restful...@googlegroups.com
You may also, call the method trowError() at your DSL. It will add a
feature on the Stack which verify some status and trow an Exception
for that error.

try {
Response response = Restfulie.at(uri).throwError().get();
} catch (BadRequestException ex) // when statuscode 400 {
// do something

Guilherme Silveira

unread,
Feb 8, 2011, 8:57:33 AM2/8/11
to restful...@googlegroups.com
Hi Doni,

Should we support:

Restfulie.at(uri).with(throwError()).get()?

Regards

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

Jose Donizetti

unread,
Feb 8, 2011, 9:15:34 AM2/8/11
to restful...@googlegroups.com
Hi gui,

I was working on something like that, for when someone create an
custom feature. He would have to implement the interface and load it
to the stack as:

Restfulie.at().withFeatures(MyCustom.feature).get(); // with() is much
better then withFeatures();

We could use it for our built-in features too, as you said, so that
we won't have to change our fluent interface every time a new feature
appear. The only 'problem' that comes to my mind, is the fact that
every method would have to be static.

Doni.


On Tue, Feb 8, 2011 at 11:57 AM, Guilherme Silveira

Guilherme Silveira

unread,
Feb 8, 2011, 9:31:30 AM2/8/11
to restful...@googlegroups.com
Hi Doni,

Just the loookup method, no problem bringing functional aspects to
Java to be able to create a DSL.

public Request with(Feature) {
load into stack
return itself
}

BTW, throwing exceptions is not considered that Restful... the return
code should always be verified in order to do something. Its more
clever to add callbacks in specific situations (i.e. 503, try again
later) than just throwing an exception.

Regards

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

Jose Donizetti

unread,
Feb 8, 2011, 9:50:23 AM2/8/11
to restful...@googlegroups.com
Cool, I will refactoring it to work in such way. About the callbacks
there are another features like retryWhenUnavaiable that retry an
request, when it fails. At the moment it just verify the 503 code, but
it can be extended to other codes (a port from your version at the
restfulie-ruby). And such callbacks makes sense for codes like 503 and
406. But codes like 401 and 403 that involves permission, does it make
sense to try again? For such situations, would be something
interesting than throwing an exception?

Doni.

On Tue, Feb 8, 2011 at 12:31 PM, Guilherme Silveira

Guilherme Silveira

unread,
Feb 8, 2011, 10:46:43 AM2/8/11
to restful...@googlegroups.com
Hi Doni,

There is probably something more interested that can be built around
Restfulie, such as automatically asking the user for permission if its
a desktop app, or redirecting to the oauth provider if its an oauth
based app... but those can be left for later, under someone's request

Regards
Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

On Tue, Feb 8, 2011 at 12:50 PM, Jose Donizetti

Jose Donizetti

unread,
Feb 8, 2011, 11:35:49 AM2/8/11
to restful...@googlegroups.com
Sure, I was just understanding it. :)

On Tue, Feb 8, 2011 at 1:46 PM, Guilherme Silveira

RafaelViana

unread,
Feb 9, 2011, 5:38:51 AM2/9/11
to restfulie-java
Some statuses allows strings. Can this be used to send the error
message?

result.use(status()).badRequest("Error message");

Can I get this message at the client?

On 8 fev, 11:17, Guilherme Silveira <guilherme.silve...@caelum.com.br>
wrote:
> Hi Rafael,
>
> In the client you can do:
>
> Response r = Restfulie.at(uri).get();
> System.out.println(r.getCode());
>
> Regards
>
> Guilherme Silveira
> Caelum | Ensino e Inovaçãohttp://www.caelum.com.br/

Guilherme Silveira

unread,
Feb 9, 2011, 7:07:43 AM2/9/11
to restful...@googlegroups.com
Hi Rafael,

The exception message (or error message) should be serialized in the
body, in xml, json,text/plain or html, as you wish, like in:

400 Bad Request
Content-type: application/json
{ 'error' : 'message' }

Can you do that?

The reason is, according to the specs this is an example of a http response:

400 Bad Request

In this case, the status line *must* contain the correct message for
400, which is Bad Request, not any other string.

Regards

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

RafaelViana

unread,
Feb 9, 2011, 8:39:12 AM2/9/11
to restfulie-java
Hi Guilherme,

So, where goes this message put in the badRequest? Why do this method
support this constructor? Is it necessary?

To add the message in body, there is a issue opened in VRaptor to do
this: https://github.com/caelum/vraptor/issues#issue/325
I was watching it yesterday, but didn't find a way to add it without
using converters.

On 9 fev, 10:07, Guilherme Silveira <guilherme.silve...@caelum.com.br>
wrote:
> Hi Rafael,
>
> The exception message (or error message) should be serialized in the
> body, in xml, json,text/plain or html, as you wish, like in:
>
> 400 Bad Request
> Content-type: application/json
> { 'error' : 'message' }
>
> Can you do that?
>
> The reason is, according to the specs this is an example of a http response:
>
> 400 Bad Request
>
> In this case, the status line *must* contain the correct message for
> 400, which is Bad Request, not any other string.
>
> Regards
>
> Guilherme Silveira
> Caelum | Ensino e Inovaçãohttp://www.caelum.com.br/

Guilherme Silveira

unread,
Feb 9, 2011, 8:44:27 AM2/9/11
to restful...@googlegroups.com
Hi Rafael,

I am not sure, those might be two different issues.... if an error
occurs do you want to serialize data + error or just the error?
Can you show the entire method code so we can find a solution?

Regards

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

RafaelViana

unread,
Feb 9, 2011, 9:56:51 AM2/9/11
to restfulie-java
Firstly, the method public void badRequest(java.lang.String message)
in DefaultStatus.java is needed?

Secondly, In my opinion, serialize just the error would be better.I
don't have a specific method because I'm justing making some POC's.
But, for example:

public Produto salvar(Produto produto)
{
return tenant.save(produto);
}

If happen an error during the save, I need to throw it to the client.
I tried something like this:

public Produto salvar(Produto produto)
{
try {
return tenant.save(produto);
} catch(Exception ex) {
result.use(status()).badRequest(ex.getLocalizedMessage());
}
}

Your suggestion is better. Because in this case wouldn't be necessary
to serialize the model. Just the message.

I found this method:

public void badRequest(List<?> errors) {
result.use(representation()).from(errors, "errors").serialize();
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}

I think this method could solve this exception handling? I'll do a
test with it.

Finally, I'll also try to addCallback how you suggested above to
handle the exceptions.

On 9 fev, 11:44, Guilherme Silveira <guilherme.silve...@caelum.com.br>
wrote:
> Hi Rafael,
>
> I am not sure, those might be two different issues.... if an error
> occurs do you want to serialize data + error or just the error?
> Can you show the entire method code so we can find a solution?
>
> Regards
>
> Guilherme Silveira
> Caelum | Ensino e Inovaçãohttp://www.caelum.com.br/
>

Lucas Cavalcanti

unread,
Feb 9, 2011, 10:15:43 AM2/9/11
to restful...@googlegroups.com
On Wed, Feb 9, 2011 at 12:56 PM, RafaelViana <rfl....@gmail.com> wrote:
Firstly, the method public void badRequest(java.lang.String message)
in DefaultStatus.java is needed?
yes. One should be able to specify any message explaining the error.
 
Secondly, In my opinion, serialize just the error would be better.I
don't have a specific method because I'm justing making some POC's.
But, for example:

public Produto salvar(Produto produto)
{
       return tenant.save(produto);
}

you should avoid the exceptions with validations. E.g if you're using hibernate validation:

public Produto salvar(Produto produto) {
       validator.validate(produto);
       validator.onErrorSendBadRequest();
       return tenant.save(produto);
}
 
this way all errors are serialized with the method:

public void badRequest(List<?> errors) {
       result.use(representation()).from(errors, "errors").serialize();
       response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}

dont try..catch, validate!

[]'s

RafaelViana

unread,
Feb 9, 2011, 10:28:33 AM2/9/11
to restfulie-java


On 9 fev, 13:15, Lucas Cavalcanti <lucasmrtu...@gmail.com> wrote:
> On Wed, Feb 9, 2011 at 12:56 PM, RafaelViana <rfl.vi...@gmail.com> wrote:
> > Firstly, the method public void badRequest(java.lang.String message)
> > in DefaultStatus.java is needed?
>
> yes. One should be able to specify any message explaining the error.
>

That's my doubt. What is the utility if i can't catch it? (or there
are other cases that can catch it?)

> > Secondly, In my opinion, serialize just the error would be better.I
> > don't have a specific method because I'm justing making some POC's.
> > But, for example:
>
> > public Produto salvar(Produto produto)
> > {
> >        return tenant.save(produto);
> > }
>
> you should avoid the exceptions with validations. E.g if you're using
> hibernate validation:
>
> public Produto salvar(Produto produto) {
>        validator.validate(produto);
>        validator.onErrorSendBadRequest();
>        return tenant.save(produto);
>
> }
>
> this way all errors are serialized with the method:
>

I'll try it.
But, for example, If happens an exception in the save method as
ConstraintViolationException or other? How can I send a specific
message to the client explaining it?

Lucas Cavalcanti

unread,
Feb 9, 2011, 10:33:54 AM2/9/11
to restful...@googlegroups.com
> On Wed, Feb 9, 2011 at 12:56 PM, RafaelViana <rfl.vi...@gmail.com> wrote:
> > Firstly, the method public void badRequest(java.lang.String message)
> > in DefaultStatus.java is needed?
>
> yes. One should be able to specify any message explaining the error.
>

That's my doubt. What is the utility if i can't catch it? (or there
are other cases that can catch it?)
if (entityIsMalformed) {
      result.use(status()).badRequest("The entity is malformed because XYZ");
}
as I said, you don't need exceptions

> > Secondly, In my opinion, serialize just the error would be better.I
> > don't have a specific method because I'm justing making some POC's.
> > But, for example:
>
> > public Produto salvar(Produto produto)
> > {
> >        return tenant.save(produto);
> > }
>
> you should avoid the exceptions with validations. E.g if you're using
> hibernate validation:
>
> public Produto salvar(Produto produto) {
>        validator.validate(produto);
>        validator.onErrorSendBadRequest();
>        return tenant.save(produto);
>
> }
>
> this way all errors are serialized with the method:
>

I'll try it.
But, for example, If happens an exception in the save method as
ConstraintViolationException or other? How can I send a specific
message to the client explaining it?
you can always avoid ConstraintViolations with validation, and in validation
you can send any message.

[]'s 

RafaelViana

unread,
Feb 9, 2011, 10:49:06 AM2/9/11
to restfulie-java
Ok. I'll take a look later today.
Reply all
Reply to author
Forward
0 new messages