Search in sources :

Example 71 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project riposte by Nike-Inc.

the class ExceptionHandlingHandlerTest method processError_returns_value_of_processUnhandledError_if_riposteErrorHandler_returns_null.

@Test
public void processError_returns_value_of_processUnhandledError_if_riposteErrorHandler_returns_null() throws UnexpectedMajorErrorHandlingError, JsonProcessingException {
    // given
    HttpProcessingState stateMock = mock(HttpProcessingState.class);
    Object msg = new Object();
    Throwable cause = new Exception();
    ExceptionHandlingHandler handlerSpy = spy(handler);
    ResponseInfo<ErrorResponseBody> responseInfoMockFromCatchallMethod = mock(ResponseInfo.class);
    doReturn(null).when(riposteErrorHandlerMock).maybeHandleError(any(), any());
    doReturn(responseInfoMockFromCatchallMethod).when(handlerSpy).processUnhandledError(stateMock, msg, cause);
    // when
    ResponseInfo<ErrorResponseBody> response = handlerSpy.processError(stateMock, msg, cause);
    // then
    verify(riposteErrorHandlerMock).maybeHandleError(any(), any());
    assertThat(response, is(responseInfoMockFromCatchallMethod));
}
Also used : HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ErrorResponseBody(com.nike.riposte.server.error.handler.ErrorResponseBody) IncompleteHttpCallTimeoutException(com.nike.riposte.server.error.exception.IncompleteHttpCallTimeoutException) TooManyOpenChannelsException(com.nike.riposte.server.error.exception.TooManyOpenChannelsException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test)

Example 72 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project dropbox-sdk-java by dropbox.

the class DbxRequestUtil method unexpectedStatus.

public static DbxException unexpectedStatus(HttpRequestor.Response response, String userId) throws NetworkIOException, BadResponseException {
    String requestId = getRequestId(response);
    String message = null;
    DbxException networkError;
    switch(response.getStatusCode()) {
        case 400:
            message = DbxRequestUtil.messageFromResponse(response, requestId);
            networkError = new BadRequestException(requestId, message);
            break;
        case 401:
            message = DbxRequestUtil.messageFromResponse(response, requestId);
            networkError = new InvalidAccessTokenException(requestId, message);
            break;
        case 403:
            try {
                ApiErrorResponse<AccessError> accessErrorResponse = new ApiErrorResponse.Serializer<AccessError>(AccessError.Serializer.INSTANCE).deserialize(response.getBody());
                if (accessErrorResponse.getUserMessage() != null) {
                    message = accessErrorResponse.getUserMessage().toString();
                }
                AccessError accessError = accessErrorResponse.getError();
                networkError = new AccessErrorException(requestId, message, accessError);
            } catch (JsonProcessingException ex) {
                throw new BadResponseException(requestId, "Bad JSON: " + ex.getMessage(), ex);
            } catch (IOException ex) {
                throw new NetworkIOException(ex);
            }
            break;
        case 422:
            try {
                ApiErrorResponse<PathRootError> pathRootErrorResponse = new ApiErrorResponse.Serializer<PathRootError>(Serializer.INSTANCE).deserialize(response.getBody());
                if (pathRootErrorResponse.getUserMessage() != null) {
                    message = pathRootErrorResponse.getUserMessage().toString();
                }
                PathRootError pathRootError = pathRootErrorResponse.getError();
                networkError = new PathRootErrorException(requestId, message, pathRootError);
            } catch (JsonProcessingException ex) {
                throw new BadResponseException(requestId, "Bad JSON: " + ex.getMessage(), ex);
            } catch (IOException ex) {
                throw new NetworkIOException(ex);
            }
            break;
        case 429:
            try {
                int backoffSecs = Integer.parseInt(getFirstHeader(response, "Retry-After"));
                networkError = new RateLimitException(requestId, message, backoffSecs, TimeUnit.SECONDS);
                break;
            } catch (NumberFormatException ex) {
                networkError = new BadResponseException(requestId, "Invalid value for HTTP header: \"Retry-After\"");
                break;
            }
        case 500:
            networkError = new ServerException(requestId, message);
            break;
        case 503:
            // API v1 may include Retry-After in 503 responses, v2 does not
            String retryAfter = getFirstHeaderMaybe(response, "Retry-After");
            try {
                if (retryAfter != null && !retryAfter.trim().isEmpty()) {
                    int backoffSecs = Integer.parseInt(retryAfter);
                    networkError = new RetryException(requestId, message, backoffSecs, TimeUnit.SECONDS);
                    break;
                } else {
                    networkError = new RetryException(requestId, message);
                    break;
                }
            } catch (NumberFormatException ex) {
                networkError = new BadResponseException(requestId, "Invalid value for HTTP header: \"Retry-After\"");
                break;
            }
        default:
            networkError = new BadResponseCodeException(requestId, "unexpected HTTP status code: " + response.getStatusCode() + ": " + message, response.getStatusCode());
    }
    DbxGlobalCallbackFactory factory = DbxRequestUtil.sharedCallbackFactory;
    if (factory != null) {
        DbxNetworkErrorCallback callback = factory.createNetworkErrorCallback(userId);
        callback.onNetworkError(networkError);
    }
    return networkError;
}
Also used : DbxGlobalCallbackFactory(com.dropbox.core.v2.callbacks.DbxGlobalCallbackFactory) PathRootError(com.dropbox.core.v2.common.PathRootError) AccessError(com.dropbox.core.v2.auth.AccessError) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DbxNetworkErrorCallback(com.dropbox.core.v2.callbacks.DbxNetworkErrorCallback) IOException(java.io.IOException)

Example 73 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project jsonschema2pojo by joelittlejohn.

the class ContentResolver method resolveFromClasspath.

private JsonNode resolveFromClasspath(URI uri) {
    String path = removeStart(removeStart(uri.toString(), uri.getScheme() + ":"), "/");
    InputStream contentAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
    if (contentAsStream == null) {
        throw new IllegalArgumentException("Couldn't read content from the classpath, file not found: " + uri);
    }
    try {
        return OBJECT_MAPPER.readTree(contentAsStream);
    } catch (JsonProcessingException e) {
        throw new IllegalArgumentException("Error parsing document: " + uri, e);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("Unrecognised URI, can't resolve this: " + uri, e);
    } catch (IOException e) {
        throw new IllegalArgumentException("Unrecognised URI, can't resolve this: " + uri, e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 74 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project druid by druid-io.

the class LookupCoordinatorManagerTest method testParseErrorUpdateAllOnHost.

@Test
public void testParseErrorUpdateAllOnHost() throws Exception {
    final AtomicReference<List<Map<String, Object>>> configVal = new AtomicReference<>(null);
    final URL url = LookupCoordinatorManager.getLookupsURL(HostAndPort.fromString("localhost"));
    EasyMock.reset(configManager);
    EasyMock.expect(configManager.watch(EasyMock.anyString(), EasyMock.<TypeReference>anyObject())).andReturn(configVal);
    final JsonProcessingException ex = EasyMock.createStrictMock(JsonProcessingException.class);
    final ObjectMapper mapper = EasyMock.createStrictMock(ObjectMapper.class);
    EasyMock.expect(mapper.writeValueAsBytes(EasyMock.eq(SINGLE_LOOKUP_MAP))).andThrow(ex);
    expectedException.expectCause(new BaseMatcher<Throwable>() {

        @Override
        public boolean matches(Object o) {
            return ex == o;
        }

        @Override
        public void describeTo(Description description) {
        }
    });
    EasyMock.replay(mapper);
    final LookupCoordinatorManager manager = new LookupCoordinatorManager(client, discoverer, mapper, configManager, lookupCoordinatorManagerConfig);
    try {
        manager.updateAllOnHost(url, SINGLE_LOOKUP_MAP);
    } finally {
        EasyMock.verify(mapper);
    }
}
Also used : Description(org.hamcrest.Description) AtomicReference(java.util.concurrent.atomic.AtomicReference) URL(java.net.URL) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 75 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project druid by druid-io.

the class AsyncQueryForwardingServlet method sendProxyRequest.

@Override
protected void sendProxyRequest(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Request proxyRequest) {
    proxyRequest.timeout(httpClientConfig.getReadTimeout().getMillis(), TimeUnit.MILLISECONDS);
    proxyRequest.idleTimeout(httpClientConfig.getReadTimeout().getMillis(), TimeUnit.MILLISECONDS);
    final Query query = (Query) clientRequest.getAttribute(QUERY_ATTRIBUTE);
    if (query != null) {
        final ObjectMapper objectMapper = (ObjectMapper) clientRequest.getAttribute(OBJECTMAPPER_ATTRIBUTE);
        try {
            proxyRequest.content(new BytesContentProvider(objectMapper.writeValueAsBytes(query)));
        } catch (JsonProcessingException e) {
            Throwables.propagate(e);
        }
    }
    super.sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
}
Also used : Query(io.druid.query.Query) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)153 IOException (java.io.IOException)43 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)42 HashMap (java.util.HashMap)18 ArrayList (java.util.ArrayList)15 Test (org.junit.Test)14 InputStream (java.io.InputStream)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 List (java.util.List)8 Map (java.util.Map)8 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)7 ErrorResponseBody (com.nike.riposte.server.error.handler.ErrorResponseBody)6 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)6 OutputStreamWriter (java.io.OutputStreamWriter)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 TaskDTO (com.linkedin.thirdeye.datalayer.dto.TaskDTO)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 SimpleDateFormat (java.text.SimpleDateFormat)4 Test (org.testng.annotations.Test)4