Search in sources :

Example 1 with GraphErrorResponse

use of com.microsoft.graph.http.GraphErrorResponse in project msgraph-sdk-java-core by microsoftgraph.

the class BatchResponseStep method getDeserializedBody.

/**
 * Returned the deserialized response body of the current step
 * @param <T2> type of the response body
 * @param resultClass class of the resulting response body
 * @return the deserialized response body
 * @throws GraphServiceException when a bad request was sent
 * @throws GraphFatalServiceException when the service did not complete the operation as expected because of an internal error
 */
@Nullable
public <T2> T2 getDeserializedBody(@Nonnull final Class<T2> resultClass) throws GraphServiceException, GraphFatalServiceException {
    Objects.requireNonNull(resultClass, "parameter resultClass cannot be null");
    if (serializer == null || body == null || !(body instanceof JsonElement))
        return null;
    final GraphErrorResponse error = serializer.deserializeObject((JsonElement) body, GraphErrorResponse.class);
    if (error == null || error.error == null) {
        return serializer.deserializeObject((JsonElement) body, resultClass);
    } else {
        boolean verboseError = false;
        if (serializer instanceof DefaultSerializer) {
            final ILogger logger = ((DefaultSerializer) serializer).getLogger();
            verboseError = logger != null && logger.getLoggingLevel() == LoggerLevel.DEBUG;
        }
        throw GraphServiceException.createFromResponse("", "", new ArrayList<>(), "", headers, "", status, error, verboseError);
    }
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) GraphErrorResponse(com.microsoft.graph.http.GraphErrorResponse) JsonElement(com.google.gson.JsonElement) ILogger(com.microsoft.graph.logger.ILogger) Nullable(javax.annotation.Nullable)

Example 2 with GraphErrorResponse

use of com.microsoft.graph.http.GraphErrorResponse in project msgraph-sdk-java-core by microsoftgraph.

the class BatchResponseContentTest method doesNotIncludeVerboseInformation.

@Test
public void doesNotIncludeVerboseInformation() {
    String responsebody = "{\"responses\":[{\"id\":\"1\",\"status\":400,\"headers\":{\"Cache-Control\":\"no-cache\",\"x-ms-resource-unit\":\"1\",\"Content-Type\":\"application/json\"},\"body\":{\"error\":{\"code\":\"Request_BadRequest\",\"message\":\"Avalueisrequiredforproperty'displayName'ofresource'User'.\",\"innerError\":{\"date\":\"2021-02-02T19:19:38\",\"request-id\":\"408b8e64-4047-4c97-95b6-46e9f212ab48\",\"client-request-id\":\"102910da-260c-3028-0fb3-7d6903a02622\"}}}}]}";
    ISerializer serializer = new DefaultSerializer(new DefaultLogger() {

        {
            setLoggingLevel(LoggerLevel.ERROR);
        }
    });
    BatchResponseContent batchresponse = serializer.deserializeObject(responsebody, BatchResponseContent.class);
    if (batchresponse != null) {
        if (// this is done by the batch request in the fluent API
        batchresponse.responses != null)
            for (final BatchResponseStep<?> step : batchresponse.responses) {
                step.serializer = serializer;
            }
        try {
            batchresponse.getResponseById("1").getDeserializedBody(BatchRequestContent.class);
        } catch (GraphServiceException ex) {
            final GraphErrorResponse response = ex.getError();
            assertNotNull(response);
            assertNotNull(response.error);
            assertNotNull(response.error.message);
            assertEquals(ex.getMessage(false), ex.getMessage());
        }
    } else
        fail("batch response was null");
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) GraphErrorResponse(com.microsoft.graph.http.GraphErrorResponse) GraphServiceException(com.microsoft.graph.http.GraphServiceException) DefaultLogger(com.microsoft.graph.logger.DefaultLogger) ISerializer(com.microsoft.graph.serializer.ISerializer) Test(org.junit.jupiter.api.Test)

Example 3 with GraphErrorResponse

use of com.microsoft.graph.http.GraphErrorResponse in project msgraph-sdk-java-core by microsoftgraph.

the class BatchResponseContentTest method includeVerboseInformation.

@Test
public void includeVerboseInformation() {
    String responsebody = "{\"responses\":[{\"id\":\"1\",\"status\":400,\"headers\":{\"Cache-Control\":\"no-cache\",\"x-ms-resource-unit\":\"1\",\"Content-Type\":\"application/json\"},\"body\":{\"error\":{\"code\":\"Request_BadRequest\",\"message\":\"Avalueisrequiredforproperty'displayName'ofresource'User'.\",\"innerError\":{\"date\":\"2021-02-02T19:19:38\",\"request-id\":\"408b8e64-4047-4c97-95b6-46e9f212ab48\",\"client-request-id\":\"102910da-260c-3028-0fb3-7d6903a02622\"}}}}]}";
    ISerializer serializer = new DefaultSerializer(new DefaultLogger() {

        {
            setLoggingLevel(LoggerLevel.DEBUG);
        }
    });
    BatchResponseContent batchresponse = serializer.deserializeObject(responsebody, BatchResponseContent.class);
    if (batchresponse != null) {
        if (// this is done by the batch request in the fluent API
        batchresponse.responses != null)
            for (final BatchResponseStep<?> step : batchresponse.responses) {
                step.serializer = serializer;
            }
        try {
            batchresponse.getResponseById("1").getDeserializedBody(BatchRequestContent.class);
        } catch (GraphServiceException ex) {
            final GraphErrorResponse response = ex.getError();
            assertNotNull(response);
            assertNotNull(response.error);
            assertNotNull(response.error.message);
            assertEquals(ex.getMessage(true), ex.getMessage());
        }
    } else
        fail("batch response was null");
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) GraphErrorResponse(com.microsoft.graph.http.GraphErrorResponse) GraphServiceException(com.microsoft.graph.http.GraphServiceException) DefaultLogger(com.microsoft.graph.logger.DefaultLogger) ISerializer(com.microsoft.graph.serializer.ISerializer) Test(org.junit.jupiter.api.Test)

Example 4 with GraphErrorResponse

use of com.microsoft.graph.http.GraphErrorResponse in project msgraph-sdk-java-core by microsoftgraph.

the class BatchResponseContentTest method deserializesErrorsProperly.

@Test
public void deserializesErrorsProperly() {
    String responsebody = "{\"responses\":[{\"id\":\"1\",\"status\":400,\"headers\":{\"Cache-Control\":\"no-cache\",\"x-ms-resource-unit\":\"1\",\"Content-Type\":\"application/json\"},\"body\":{\"error\":{\"code\":\"Request_BadRequest\",\"message\":\"Avalueisrequiredforproperty'displayName'ofresource'User'.\",\"innerError\":{\"date\":\"2021-02-02T19:19:38\",\"request-id\":\"408b8e64-4047-4c97-95b6-46e9f212ab48\",\"client-request-id\":\"102910da-260c-3028-0fb3-7d6903a02622\"}}}}]}";
    ISerializer serializer = new DefaultSerializer(mock(ILogger.class));
    BatchResponseContent batchresponse = serializer.deserializeObject(responsebody, BatchResponseContent.class);
    if (batchresponse != null) {
        if (// this is done by the batch request in the fluent API
        batchresponse.responses != null)
            for (final BatchResponseStep<?> step : batchresponse.responses) {
                step.serializer = serializer;
            }
        assertThrows(GraphServiceException.class, () -> {
            batchresponse.getResponseById("1").getDeserializedBody(BatchRequestContent.class);
        });
        try {
            batchresponse.getResponseById("1").getDeserializedBody(BatchRequestContent.class);
        } catch (GraphServiceException ex) {
            final GraphErrorResponse response = ex.getError();
            assertNotNull(response);
            assertNotNull(response.error);
            assertNotNull(response.error.message);
        }
    } else
        fail("batch response was null");
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) GraphErrorResponse(com.microsoft.graph.http.GraphErrorResponse) GraphServiceException(com.microsoft.graph.http.GraphServiceException) ILogger(com.microsoft.graph.logger.ILogger) ISerializer(com.microsoft.graph.serializer.ISerializer) Test(org.junit.jupiter.api.Test)

Aggregations

GraphErrorResponse (com.microsoft.graph.http.GraphErrorResponse)4 DefaultSerializer (com.microsoft.graph.serializer.DefaultSerializer)4 GraphServiceException (com.microsoft.graph.http.GraphServiceException)3 ISerializer (com.microsoft.graph.serializer.ISerializer)3 Test (org.junit.jupiter.api.Test)3 DefaultLogger (com.microsoft.graph.logger.DefaultLogger)2 ILogger (com.microsoft.graph.logger.ILogger)2 JsonElement (com.google.gson.JsonElement)1 Nullable (javax.annotation.Nullable)1