use of dev.hilla.exception.EndpointException in project flow by vaadin.
the class EndpointInvoker method handleMethodExecutionError.
private ResponseEntity<String> handleMethodExecutionError(String endpointName, String methodName, InvocationTargetException e) throws JsonProcessingException {
if (EndpointException.class.isAssignableFrom(e.getCause().getClass())) {
EndpointException endpointException = ((EndpointException) e.getCause());
getLogger().debug("Endpoint '{}' method '{}' aborted the execution", endpointName, methodName, endpointException);
return ResponseEntity.badRequest().body(vaadinEndpointMapper.writeValueAsString(endpointException.getSerializationData()));
} else {
String errorMessage = String.format("Endpoint '%s' method '%s' execution failure", endpointName, methodName);
getLogger().error(errorMessage, e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(createResponseErrorObject(errorMessage));
}
}
use of dev.hilla.exception.EndpointException in project flow by vaadin.
the class EndpointControllerTest method should_Return400_When_EndpointMethodThrowsVaadinConnectExceptionSubclass.
@Test
@Ignore("requires mockito version with plugin for final classes")
public void should_Return400_When_EndpointMethodThrowsVaadinConnectExceptionSubclass() throws Exception {
int inputValue = 222;
String expectedMessage = "OOPS";
class MyCustomException extends EndpointException {
public MyCustomException() {
super(expectedMessage);
}
}
Method endpointMethodMock = createEndpointMethodMockThatThrows(inputValue, new InvocationTargetException(new MyCustomException()));
EndpointController controller = createVaadinController(TEST_ENDPOINT);
controller.endpointRegistry.get(TEST_ENDPOINT_NAME.toLowerCase()).methods.put(TEST_METHOD.getName().toLowerCase(), endpointMethodMock);
ResponseEntity<String> response = controller.serveEndpoint(TEST_ENDPOINT_NAME, TEST_METHOD.getName(), createRequestParameters(String.format("{\"value\": %s}", inputValue)), requestMock);
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
String responseBody = response.getBody();
assertTrue(String.format("Invalid response body: '%s'", responseBody), responseBody.contains(MyCustomException.class.getName()));
assertTrue(String.format("Invalid response body: '%s'", responseBody), responseBody.contains(expectedMessage));
verify(endpointMethodMock, times(1)).invoke(TEST_ENDPOINT, inputValue);
verify(endpointMethodMock, times(1)).getParameters();
}
use of dev.hilla.exception.EndpointException in project flow by vaadin.
the class EndpointControllerTest method should_Return400_When_EndpointMethodThrowsVaadinConnectException.
@Test
@Ignore("requires mockito version with plugin for final classes")
public void should_Return400_When_EndpointMethodThrowsVaadinConnectException() throws Exception {
int inputValue = 222;
String expectedMessage = "OOPS";
Method endpointMethodMock = createEndpointMethodMockThatThrows(inputValue, new InvocationTargetException(new EndpointException(expectedMessage)));
EndpointController controller = createVaadinController(TEST_ENDPOINT);
controller.endpointRegistry.get(TEST_ENDPOINT_NAME.toLowerCase()).methods.put(TEST_METHOD.getName().toLowerCase(), endpointMethodMock);
ResponseEntity<String> response = controller.serveEndpoint(TEST_ENDPOINT_NAME, TEST_METHOD.getName(), createRequestParameters(String.format("{\"value\": %s}", inputValue)), requestMock);
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
String responseBody = response.getBody();
assertTrue(String.format("Invalid response body: '%s'", responseBody), responseBody.contains(EndpointException.class.getName()));
assertTrue(String.format("Invalid response body: '%s'", responseBody), responseBody.contains(expectedMessage));
verify(endpointMethodMock, times(1)).invoke(TEST_ENDPOINT, inputValue);
verify(endpointMethodMock, times(1)).getParameters();
}
use of dev.hilla.exception.EndpointException 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));
}
Aggregations