Skip to content

Commit

Permalink
POST Forms Messages and Validations (#70)
Browse files Browse the repository at this point in the history
- return errors in forms with invalid constraints (via POST calls to the API) as a JSON body with the errors mapped as "field":"message"
- return a 400 Bad Request code in case a field in the Path Params and the Query Params (via GET calls to the API) instead of returning a 500 Internal Server Error with a 404 Not found body message
- return a 400 Bad Request code in forms with invalid constraints instead of a 500 Internal Server Error
- added custom (spanish) messages for constraints validation errors
  • Loading branch information
gibarsin committed Feb 2, 2017
1 parent 90d75e4 commit 324b9fb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ar.edu.itba.paw.webapp.exceptions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class ConstraintViolationExceptionMapper implements ExceptionMapper<ConstraintViolationException> {

private static final Logger LOGGER = LoggerFactory.getLogger(ConstraintViolationExceptionMapper.class);

private static final int LEAF_POSITION = 2;

@Override
public Response toResponse(final ConstraintViolationException e) {
LOGGER.warn("Exception: {}", (Object[]) e.getStackTrace());
final StringBuilder responseEntity = new StringBuilder();

responseEntity.append("{ errors: [\n");
for (ConstraintViolation<?> cv : e.getConstraintViolations()) {
final String propertyName;
final String[] propertyNodes = cv.getPropertyPath().toString().split("\\.");

if(propertyNodes.length == 3) {
propertyName = propertyNodes[LEAF_POSITION];
} else {
propertyName = propertyNodes[1];
}

LOGGER.debug("propertyname = " + propertyName);

responseEntity.append("{");
responseEntity
.append("\"")
.append(propertyName)
.append("\"")
.append(":")
.append("\"")
.append(cv.getMessage())
.append("\"")
.append("},")
.append("\n");
}
responseEntity.append("] }");
return Response.status(Response.Status.BAD_REQUEST).entity(responseEntity.toString()).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ar.edu.itba.paw.webapp.exceptions;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import static org.glassfish.jersey.server.ParamException.QueryParamException;

@Provider
public class QueryParamExceptionMapper implements ExceptionMapper<QueryParamException> {
@Override
public Response toResponse(final QueryParamException exception) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
}

This file was deleted.

14 changes: 6 additions & 8 deletions webapp/src/main/resources/ValidationMessages.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Jersey Bean Validations

javax.validation.constraints.Min.message=must be greater than or equal to {value}
javax.validation.constraints.Digits=must have {integer} digits
org.hibernate.validator.constraints.NotBlank=must not be empty
javax.validation.constraints.NotNull=must not be null
javax.validation.constraints.Size=must be between {min} and {max} characters long
javax.validation.constraints.Pattern=must follow the pattern: {regexp}
javax.validation.constraints.Min.message=debe ser igual o mayor a {value}
javax.validation.constraints.Digits.message=debe poseer {integer} dígitos
org.hibernate.validator.constraints.NotBlank.message=no debe estar vacío
javax.validation.constraints.NotNull.message=no debe ser nulo
javax.validation.constraints.Size.message=debe tener entre {min} y {max} caracteres
javax.validation.constraints.Pattern.message=debe poseer la siguiente expresión regular: {regexp}

0 comments on commit 324b9fb

Please sign in to comment.