David Choi <daft.davy <at> gmail.com> writes:
>
> Hello,
>
> When constructing DOM documents to create DOM Representations (or, for that
> matter, JSON Objects for JSONRepresentations), I need to return a 500 Internal
> Server Error when DOM Document/JSON Object creation throws an exception.
>
> For example:
> JSONObject jObj = new JSONObject();
> try
> {
> jObj.put("blah", blah);
> ...
> }
> catch(JSONException e)
> {
> //something needed here?
> }
>
> What is the best way to do this? Currently, if the exception occurs, I just
> skip adding the representation to the list of variants returned by
> getVariants(), but this returns a 404 - Resource Not Found.
>
> Thanks,
> David
>
>
Why not simply create a Fiter that has a try/catch block in the doHandle()
method? The try block would simply pass control to the next Restlet, and if
somewhere downstream a Handler throws a particular unchecked exception, the
filter will do the proper handling. This of course assumes a downstream restlet
isn't arbitrarily catching unexpected Throwables (which probably isn't a good
idea anyway).