Search in sources :

Example 1 with RestLiDecodingException

use of com.linkedin.restli.client.RestLiDecodingException in project rest.li by linkedin.

the class MultiplexedCallback method notifyIndividualCallbacks.

private void notifyIndividualCallbacks(Response<MultiplexedResponseContent> muxResponse) {
    IndividualResponseMap individualResponses = muxResponse.getEntity().getResponses();
    for (IndividualResponseMap.Entry<String, IndividualResponse> individualResponseMapEntry : individualResponses.entrySet()) {
        Integer id = Integer.valueOf(individualResponseMapEntry.getKey());
        IndividualResponse individualResponse = individualResponseMapEntry.getValue();
        Callback<RestResponse> callback = _callbacks.get(id);
        RestResponse individualRestResponse;
        try {
            individualRestResponse = buildIndividualRestResponse(muxResponse, individualResponse);
        } catch (MimeTypeParseException e) {
            callback.onError(new RestLiDecodingException("Could not convert IndividualResponse to individual RestRestponse due to an invalid content type, id=" + id, e));
            return;
        } catch (IOException e) {
            callback.onError(new RestLiDecodingException("Could not convert IndividualResponse to individual RestRestponse, id=" + id, e));
            return;
        }
        if (RestStatus.isOK(individualResponse.getStatus())) {
            callback.onSuccess(individualRestResponse);
        } else {
            RestException exception = new RestException(individualRestResponse, "Received error " + individualRestResponse.getStatus());
            callback.onError(exception);
        }
    }
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestException(com.linkedin.r2.message.rest.RestException) RestLiDecodingException(com.linkedin.restli.client.RestLiDecodingException) ByteString(com.linkedin.data.ByteString) IOException(java.io.IOException) IndividualResponseMap(com.linkedin.restli.common.multiplexer.IndividualResponseMap) IndividualResponse(com.linkedin.restli.common.multiplexer.IndividualResponse)

Example 2 with RestLiDecodingException

use of com.linkedin.restli.client.RestLiDecodingException in project rest.li by linkedin.

the class TestMultiplexedCallback method testError.

@Test
public void testError() throws Exception {
    FutureCallback<RestResponse> callback1 = new FutureCallback<RestResponse>();
    FutureCallback<RestResponse> callback2 = new FutureCallback<RestResponse>();
    ImmutableMap<Integer, Callback<RestResponse>> individualCallbacks = ImmutableMap.<Integer, Callback<RestResponse>>of(ID1, callback1, ID2, callback2);
    FutureCallback<MultiplexedResponse> aggregatedCallback = new FutureCallback<MultiplexedResponse>();
    MultiplexedCallback multiplexedCallback = new MultiplexedCallback(individualCallbacks, aggregatedCallback);
    RestLiDecodingException exception = new RestLiDecodingException(null, null);
    multiplexedCallback.onError(exception);
    Assert.assertSame(getError(callback1), exception);
    Assert.assertSame(getError(callback2), exception);
    Assert.assertSame(getError(aggregatedCallback), exception);
}
Also used : FutureCallback(com.linkedin.common.callback.FutureCallback) Callback(com.linkedin.common.callback.Callback) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestLiDecodingException(com.linkedin.restli.client.RestLiDecodingException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 3 with RestLiDecodingException

use of com.linkedin.restli.client.RestLiDecodingException in project rest.li by linkedin.

the class ExceptionUtil method exceptionForThrowable.

public static RemoteInvocationException exceptionForThrowable(Throwable e, RestResponseDecoder<?> responseDecoder) {
    if (e instanceof RestException) {
        final RestException re = (RestException) e;
        final RestResponse response = re.getResponse();
        final ErrorResponse errorResponse;
        // decode the response body when HEADER_RESTLI_ERROR_RESPONSE header is set.
        try {
            errorResponse = getErrorResponse(response);
        } catch (RestLiDecodingException decodingException) {
            return new RemoteInvocationException(e.getMessage(), decodingException);
        }
        Response<?> decodedResponse = null;
        final String header = HeaderUtil.getErrorResponseHeaderValue(response.getHeaders());
        if (header == null) {
            // This is purely to handle case #2 commented above.
            try {
                decodedResponse = responseDecoder.decodeResponse(response);
            } catch (RestLiDecodingException decodingException) {
                return new RemoteInvocationException(e.getMessage(), e);
            }
        }
        return new RestLiResponseException(response, decodedResponse, errorResponse, e);
    }
    if (e instanceof RemoteInvocationException) {
        return (RemoteInvocationException) e;
    }
    return new RemoteInvocationException(e);
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestException(com.linkedin.r2.message.rest.RestException) RestLiDecodingException(com.linkedin.restli.client.RestLiDecodingException) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 4 with RestLiDecodingException

use of com.linkedin.restli.client.RestLiDecodingException in project rest.li by linkedin.

the class ExceptionUtil method wrapThrowable.

static RemoteInvocationException wrapThrowable(Throwable e) {
    if (e instanceof RestLiResponseException) {
        final RestLiResponseException restliException = (RestLiResponseException) e;
        final ErrorResponse errorResponse;
        try {
            errorResponse = getErrorResponse(restliException.getResponse());
        } catch (RestLiDecodingException decodingException) {
            return new RemoteInvocationException(decodingException);
        }
        return new RestLiResponseException(restliException.getResponse(), restliException.getDecodedResponse(), errorResponse, restliException);
    }
    return new RemoteInvocationException(e);
}
Also used : RestLiDecodingException(com.linkedin.restli.client.RestLiDecodingException) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 5 with RestLiDecodingException

use of com.linkedin.restli.client.RestLiDecodingException in project rest.li by linkedin.

the class RestResponseDecoder method createResponse.

private ResponseImpl<T> createResponse(Map<String, String> headers, int status, ByteString entity, List<String> cookies) throws RestLiDecodingException {
    ResponseImpl<T> response = new ResponseImpl<T>(status, headers, CookieUtil.decodeSetCookies(cookies));
    try {
        DataMap dataMap = (entity.isEmpty()) ? null : DataMapConverter.bytesToDataMap(headers, entity);
        response.setEntity(wrapResponse(dataMap, headers, ProtocolVersionUtil.extractProtocolVersion(response.getHeaders())));
        return response;
    } catch (MimeTypeParseException e) {
        throw new RestLiDecodingException("Could not decode REST response", e);
    } catch (IOException e) {
        throw new RestLiDecodingException("Could not decode REST response", e);
    } catch (InstantiationException e) {
        throw new IllegalStateException(e);
    } catch (IllegalAccessException e) {
        throw new IllegalStateException(e);
    } catch (InvocationTargetException e) {
        throw new IllegalStateException(e);
    } catch (NoSuchMethodException e) {
        throw new IllegalStateException(e);
    }
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) RestLiDecodingException(com.linkedin.restli.client.RestLiDecodingException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataMap(com.linkedin.data.DataMap)

Aggregations

RestLiDecodingException (com.linkedin.restli.client.RestLiDecodingException)6 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)3 RestResponse (com.linkedin.r2.message.rest.RestResponse)3 IOException (java.io.IOException)3 MimeTypeParseException (javax.activation.MimeTypeParseException)3 ByteString (com.linkedin.data.ByteString)2 RestException (com.linkedin.r2.message.rest.RestException)2 RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)2 ErrorResponse (com.linkedin.restli.common.ErrorResponse)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Callback (com.linkedin.common.callback.Callback)1 FutureCallback (com.linkedin.common.callback.FutureCallback)1 DataMap (com.linkedin.data.DataMap)1 MultiPartMIMEReader (com.linkedin.multipart.MultiPartMIMEReader)1 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)1 IndividualResponse (com.linkedin.restli.common.multiplexer.IndividualResponse)1 IndividualResponseMap (com.linkedin.restli.common.multiplexer.IndividualResponseMap)1 ContentType (javax.mail.internet.ContentType)1 ParseException (javax.mail.internet.ParseException)1 Test (org.testng.annotations.Test)1