views:

133

answers:

2

I'd like my Restlet application to log the stack trace for any Resource that generates a 500-series HTTP error (using the Context's Logger). As far as I can tell, this is not the default behavior.

In other words, I don't want my Resource classes to have any logger code at all in the represent and acceptRepresentation methods - they just throw a ResourceException. I want the logging logic to be handled in a centralized way across all Resource instances.

I can think of a couple of hackish ways of doing this (e.g., inherit from a Resource subclass that handles the logging logic), but it just seems like this is something that should be built-in to the framework.

What am I missing?

+3  A: 

Take a look at StatusService:

Service to handle error statuses. If an exception is thrown within your application or Restlet code, it will be intercepted by this service if it is enabled. When an exception or an error is caught, the getStatus(Throwable, Request, Response) method is first invoked to obtain the status that you want to set on the response. [...]

I just discovered this last month and it allowed me to really compress a lot of my error handling code and also to ensure that exceptions were being handled, and being handled uniformly.

Jim Ferrans
@Jim - many thanks, this is what I was looking for. Looks like StatusService doesn't catch a ResourceException with a 500 status that you throw yourself, though. That may be where I was going wrong.
Rich Apodaca
Rich, I created a subclass of RuntimeException and threw that from a bunch of places. I also created a subclass of StatusService that caught the ones I wanted handled. That all worked for me. (Not quite sure if that's the same case you have though.)
Jim Ferrans
A: 

You might be able to use a filter to do this; in afterHandle() it could check the status code, and then try to log the exception. I'm not sure whether the exceptions are available to the filters though.

Avi Flax