Hi David,
It could be even shorter:
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND, e);
However, it seems to me that using the ResourceException, is even
simpler and more inline with the higher-level API exposed by the
Resource class, especially the *representation() methods.
Best regards,
Jérôme Louvel
--
Restlet ~ Founder and Lead developer ~ http://www.restlet.org
<http://www.restlet.org/>
Noelios Technologies ~ Co-founder ~ http://www.noelios.com
<http://www.noelios.com/>
??? a écrit :
Hi,
There is the other way,
<code>
public void acceptRepresentation( Representation entity )
throws ResourceException {
try {
// Set the response's status and entity
} catch ( Exception e ) {
getResponse().setStatus( new Status(
Status.CLIENT_ERROR_NOT_FOUND, e ) );
getResponse().setEntity( null ) ;
}
</code>
It will take the status to clients with the exception.
2008/8/23, Michael Dunn <[EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]>>:
David,
Thanks for your response. I should have been more clear. I was
wondering the
proper way from a service standpoint to return an error.
For example when inheriting from Resource in the server, - if the
following code
generates an exception - is it better to return an error via the
response or
throw an exception?
<code>
public void acceptRepresentation(Representation entity)
throws ResourceException {
try {
// Set the response's status and entity
} catch (Exception e) {
// OPTION 1
// which is the best way to return an error?????
// set it in the response as follows
response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
response.setEntity("Error", MediaType.TEXT_PLAIN)
// OR
// OPTION 2
throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND,
"Error", e);
}
}
</code>
--
Jerome Louvel
http://www.noelios.com