Skip to content

Commit ae65f04

Browse files
committed
update resource validation exception implementation
1 parent 0a5e4b3 commit ae65f04

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

EVAM04_EMISIONVOTO_API/src/main/java/edu/cibertec/votoelectronico/resource/SimpleVotoElectronicoResource.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import javax.annotation.PostConstruct;
1010
import javax.validation.ConstraintViolation;
11+
import javax.validation.ConstraintViolationException;
1112
import javax.validation.Validation;
1213
import javax.validation.Validator;
1314
import javax.validation.ValidatorFactory;
@@ -88,18 +89,13 @@ public Response obtenerResultados(String grupoPolitico) {
8889
}
8990

9091
@Override
91-
public Response emitirVoto(EmisionVotoDto resource) {
92+
public Response emitirVoto(EmisionVotoDto resource) throws ConstraintViolationException {
9293
try {
9394
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
9495
Validator validator = factory.getValidator();
9596
Set<ConstraintViolation<EmisionVotoDto>> violations = validator.validate(resource);
9697
if (violations.size() > 0) {
97-
StringBuilder errors = new StringBuilder();
98-
for (ConstraintViolation<EmisionVotoDto> violation : violations) {
99-
errors.append(violation.getMessage() + ", ");
100-
}
101-
return Response.status(Response.Status.BAD_REQUEST).entity(new EmisionVotoResponse(errors.toString()))
102-
.build();
98+
throw new ConstraintViolationException(violations);
10399
}
104100

105101
GrupoPolitico grupoPolitico = this.grupoPoliticoService.findByName(resource.getGrupoPolitico());
@@ -112,6 +108,8 @@ public Response emitirVoto(EmisionVotoDto resource) {
112108
this.votoService.emitirVoto(voto);
113109
VotoDto votoDto = this.mapper.convertFrom(voto, VotoDto.class);
114110
return Response.status(Response.Status.CREATED).entity(new EmisionVotoResponse(votoDto)).build();
111+
} catch (ConstraintViolationException e) {
112+
throw e;
115113
} catch (Exception e) {
116114
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
117115
.entity(new EmisionVotoResponse(e.getMessage())).build();

0 commit comments

Comments
 (0)