users@jersey.java.net

[Jersey] Re: Best practices for returning error information as JSON?

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Mon, 14 Feb 2011 18:24:14 -0800

On Mon, Feb 14, 2011 at 3:14 PM, Tauren Mills <tauren_at_groovee.com> wrote:
...
> So, when a validation error happens, I'm sending back a BAD_REQUEST response
> code. Perhaps there is a better code for validation errors.

I think this makes sense, since while it is broad, there doesn't seem
to be much better choices.

> I'd like to also send back a JSON object that describes the violations. Is
> there a standard format for this object, or any best practices?  I'm
> planning to create a pojo, such as ValidationViolations that contains all
> the I18N messages from within each ConstraintViolation. The methods I would
> use to build this are shown here:
> http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html/validator-usingvalidator.html#d0e651
> But is there a recommended format for error payload? What kind of
> information is typically sent back? How much information is useful? For
> instance, something like this would give me lots of information, but it
> might be too verbose:

I wouldn't worry too much about verboseness up-front. Information can
be useful for trouble-shooting; and can be trimmed down if there are
actual problems. And you will probably find that more information is
needed, to add something not yet included.
My experience has been that what really matters is following up on how
things work and adjust accordingly. It is hard (or impossible) to get
things perfect the first time; so take your best guess and refine as
needed (use tight feedback loop).

So your approach sounds good to me,

-+ Tatu +-