Search in sources :

Example 1 with EndpointException

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));
    }
}
Also used : EndpointException(dev.hilla.exception.EndpointException)

Example 2 with EndpointException

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();
}
Also used : EndpointException(dev.hilla.exception.EndpointException) Method(java.lang.reflect.Method) PersonEndpoint(dev.hilla.generator.endpoints.superclassmethods.PersonEndpoint) BridgeMethodTestEndpoint(dev.hilla.testendpoint.BridgeMethodTestEndpoint) IterableEndpoint(dev.hilla.generator.endpoints.iterableendpoint.IterableEndpoint) InvocationTargetException(java.lang.reflect.InvocationTargetException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with EndpointException

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();
}
Also used : EndpointException(dev.hilla.exception.EndpointException) Method(java.lang.reflect.Method) PersonEndpoint(dev.hilla.generator.endpoints.superclassmethods.PersonEndpoint) BridgeMethodTestEndpoint(dev.hilla.testendpoint.BridgeMethodTestEndpoint) IterableEndpoint(dev.hilla.generator.endpoints.iterableendpoint.IterableEndpoint) InvocationTargetException(java.lang.reflect.InvocationTargetException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with EndpointException

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));
}
Also used : EndpointValidationException(dev.hilla.exception.EndpointValidationException) JsonNode(com.fasterxml.jackson.databind.JsonNode) InvocationTargetException(java.lang.reflect.InvocationTargetException) Type(java.lang.reflect.Type) EndpointException(dev.hilla.exception.EndpointException) ConstraintViolation(javax.validation.ConstraintViolation) EndpointAccessChecker(dev.hilla.auth.EndpointAccessChecker)

Aggregations

EndpointException (dev.hilla.exception.EndpointException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 IterableEndpoint (dev.hilla.generator.endpoints.iterableendpoint.IterableEndpoint)2 PersonEndpoint (dev.hilla.generator.endpoints.superclassmethods.PersonEndpoint)2 BridgeMethodTestEndpoint (dev.hilla.testendpoint.BridgeMethodTestEndpoint)2 Method (java.lang.reflect.Method)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 EndpointAccessChecker (dev.hilla.auth.EndpointAccessChecker)1 EndpointValidationException (dev.hilla.exception.EndpointValidationException)1 Type (java.lang.reflect.Type)1 ConstraintViolation (javax.validation.ConstraintViolation)1