-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POST Forms Messages and Validations (#70)
- 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
Showing
4 changed files
with
74 additions
and
36 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...p/src/main/java/ar/edu/itba/paw/webapp/exceptions/ConstraintViolationExceptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
webapp/src/main/java/ar/edu/itba/paw/webapp/exceptions/QueryParamExceptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
28 changes: 0 additions & 28 deletions
28
webapp/src/main/java/ar/edu/itba/paw/webapp/exceptions/ValidationExceptionMapper.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
|