use of dev.hilla.exception.EndpointValidationException in project flow by vaadin.
the class EndpointInvoker method invokeVaadinEndpointMethod.
private ResponseEntity<String> invokeVaadinEndpointMethod(String endpointName, String methodName, Method methodToInvoke, ObjectNode body, VaadinEndpointData vaadinEndpointData, HttpServletRequest request) throws JsonProcessingException {
EndpointAccessChecker accessChecker = getAccessChecker(request.getServletContext());
String checkError = accessChecker.check(methodToInvoke, request);
if (checkError != null) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(createResponseErrorObject(String.format("Endpoint '%s' method '%s' request cannot be accessed, reason: '%s'", endpointName, methodName, checkError)));
}
Map<String, JsonNode> requestParameters = getRequestParameters(body);
Type[] javaParameters = getJavaParameters(methodToInvoke, ClassUtils.getUserClass(vaadinEndpointData.getEndpointObject()));
if (javaParameters.length != requestParameters.size()) {
return ResponseEntity.badRequest().body(createResponseErrorObject(String.format("Incorrect number of parameters for endpoint '%s' method '%s', " + "expected: %s, got: %s", endpointName, methodName, javaParameters.length, requestParameters.size())));
}
Object[] vaadinEndpointParameters;
try {
vaadinEndpointParameters = getVaadinEndpointParameters(requestParameters, javaParameters, methodName, endpointName);
} catch (EndpointValidationException e) {
getLogger().debug("Endpoint '{}' method '{}' received invalid response", endpointName, methodName, e);
return ResponseEntity.badRequest().body(vaadinEndpointMapper.writeValueAsString(e.getSerializationData()));
}
Set<ConstraintViolation<Object>> methodParameterConstraintViolations = validator.forExecutables().validateParameters(vaadinEndpointData.getEndpointObject(), methodToInvoke, vaadinEndpointParameters);
if (!methodParameterConstraintViolations.isEmpty()) {
return ResponseEntity.badRequest().body(vaadinEndpointMapper.writeValueAsString(new EndpointValidationException(String.format("Validation error in endpoint '%s' method '%s'", endpointName, methodName), createMethodValidationErrors(methodParameterConstraintViolations)).getSerializationData()));
}
Object returnValue;
try {
returnValue = methodToInvoke.invoke(vaadinEndpointData.getEndpointObject(), vaadinEndpointParameters);
} catch (IllegalArgumentException e) {
String errorMessage = String.format("Received incorrect arguments for endpoint '%s' method '%s'. " + "Expected parameter types (and their order) are: '[%s]'", endpointName, methodName, listMethodParameterTypes(javaParameters));
getLogger().debug(errorMessage, e);
return ResponseEntity.badRequest().body(createResponseErrorObject(errorMessage));
} catch (IllegalAccessException e) {
String errorMessage = String.format("Endpoint '%s' method '%s' access failure", endpointName, methodName);
getLogger().error(errorMessage, e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(createResponseErrorObject(errorMessage));
} catch (InvocationTargetException e) {
return handleMethodExecutionError(endpointName, methodName, e);
}
returnValue = endpointTransferMapper.toTransferType(returnValue);
String implicitNullError = this.explicitNullableTypeChecker.checkValueForAnnotatedElement(returnValue, methodToInvoke);
if (implicitNullError != null) {
EndpointException returnValueException = new EndpointException(String.format("Unexpected return value in endpoint '%s' method '%s'. %s", endpointName, methodName, implicitNullError));
getLogger().error(returnValueException.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(vaadinEndpointMapper.writeValueAsString(returnValueException.getSerializationData()));
}
Set<ConstraintViolation<Object>> returnValueConstraintViolations = validator.forExecutables().validateReturnValue(vaadinEndpointData.getEndpointObject(), methodToInvoke, returnValue);
if (!returnValueConstraintViolations.isEmpty()) {
getLogger().error("Endpoint '{}' method '{}' had returned a value that has validation errors: '{}', this might cause bugs on the client side. Fix the method implementation.", endpointName, methodName, returnValueConstraintViolations);
}
return ResponseEntity.ok(vaadinEndpointMapper.writeValueAsString(returnValue));
}
use of dev.hilla.exception.EndpointValidationException in project flow by vaadin.
the class EndpointInvoker method getInvalidEndpointParametersException.
private EndpointValidationException getInvalidEndpointParametersException(String methodName, String endpointName, Map<String, String> deserializationErrors, Set<ConstraintViolation<Object>> constraintViolations) {
List<ValidationErrorData> validationErrorData = new ArrayList<>(deserializationErrors.size() + constraintViolations.size());
for (Map.Entry<String, String> deserializationError : deserializationErrors.entrySet()) {
String message = String.format("Unable to deserialize an endpoint method parameter into type '%s'", deserializationError.getValue());
validationErrorData.add(new ValidationErrorData(message, deserializationError.getKey()));
}
validationErrorData.addAll(createBeanValidationErrors(constraintViolations));
String message = String.format("Validation error in endpoint '%s' method '%s'", endpointName, methodName);
return new EndpointValidationException(message, validationErrorData);
}
Aggregations