mavarazy
mavarazy

Reputation: 7735

What is the best ERROR model for REST

Is there a standard error message model for REST services?

I'm building a REST service, with Spring REST backend, and Backbone JS based frontend. On all projects, that I was there were some home made error messages mechanism, with home made client interpretation of those errors. This usually had a limited scope and use for this specific project.

Now in my current project I came to a point, where I want to introduce field errors for invalid JSON fields in POST requests. So I need to extend my model to support this.

From my point of view, it's yet another bicycle, since field validation is pretty standard requirement our days, so I wonder, if there is already an Error model, that I could reuse in my project.

I'm well familiar with HandlerExceptionResolver in Spring, and @Valid annotation in Java. In fact I'm using them allot. This is more of an architecture kind Question, what is the best model to communication this kind of errors to an independent JS client.

Upvotes: 0

Views: 547

Answers (2)

Khalid
Khalid

Reputation: 2230

What I usually do is create a ValidationError model. The model is returned to the client as JSON in the case of a validation error, along with an HTTP status code, and it contains everything the client needs to know about the error.

The specifics of the model varies, but something like this would be a good start:

public class ValidationError {
    private int code;

    private String message;

    private Map<String, String> fields;

    /*...*/
}

Now when an invalid form is submitted, the JSON response will be something like:

{
  "code": 400,
  "message": "invalid form value(s)",
  "fields": {
    "username": "the username already exists",
    "password": "password must be more than 6 characters"
  }
}

This ties up nicely with Spring's @ExceptionHandler. When you get something like a BindException, all you have to do is:

@ExceptionHandler(BindException.class)
@ResponseBody
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
private ValidationError handleBindException(BindException ex) {
    ValidationError validationError = new ValidationError();
    /* set code and message */
    for (FieldError error : ex.getFieldErrors()) {
        /* set fields' errors */
    }
    return validationError;
}

BindException makes it easy to get the fields' errors. More work is needed to dig up the errors from exceptions like JsonMappingException.

Upvotes: 4

cs_stackX
cs_stackX

Reputation: 1527

Presumably the best approach is validation both on the server and on the client. I'll limit my advice to the client, as I'm not familiar with Spring: Backbone does have a validate method for use with your Backbone models. The Backbone docs encourage you to override this method with your own validation logic. validate gets called by default whenever you do a backbone model save (i.e. POST), and you can also trigger the invalid event whenever you want to run the validate code (for example, after a key-up, before the user clicks the submit button).

If you're not keen on writing a lot of custom front-end validation, there are some plugins available, for example backbone.validation.

Upvotes: 1

Related Questions