Search in sources :

Example 1 with ErrorResponse

use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.

the class IndividualResponseException method createErrorResponse.

private static ErrorResponse createErrorResponse(HttpStatus status, String message) {
    ErrorResponse errorResponse = new ErrorResponse();
    errorResponse.setStatus(status.getCode());
    errorResponse.setMessage(message);
    return errorResponse;
}
Also used : ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 2 with ErrorResponse

use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.

the class RestClientTest method testRestLiResponseExceptionCallback.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "sendRequestOptions")
public void testRestLiResponseExceptionCallback(SendRequestOption option, TimeoutOption timeoutOption, ProtocolVersionOption versionOption, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws ExecutionException, TimeoutException, InterruptedException, RestLiDecodingException {
    final String ERR_KEY = "someErr";
    final String ERR_VALUE = "WHOOPS!";
    final String ERR_MSG = "whoops2";
    final int HTTP_CODE = 400;
    final int APP_CODE = 666;
    RestClient client = mockClient(ERR_KEY, ERR_VALUE, ERR_MSG, HTTP_CODE, APP_CODE, protocolVersion, errorResponseHeaderName);
    Request<EmptyRecord> request = mockRequest(EmptyRecord.class, versionOption);
    RequestBuilder<Request<EmptyRecord>> requestBuilder = mockRequestBuilder(request);
    FutureCallback<Response<EmptyRecord>> callback = new FutureCallback<Response<EmptyRecord>>();
    try {
        sendRequest(option, client, request, requestBuilder, callback);
        Long l = timeoutOption._l;
        TimeUnit timeUnit = timeoutOption._timeUnit;
        Response<EmptyRecord> response = l == null ? callback.get() : callback.get(l, timeUnit);
        Assert.fail("Should have thrown");
    } catch (ExecutionException e) {
        // New
        Throwable cause = e.getCause();
        Assert.assertTrue(cause instanceof RestLiResponseException, "Expected RestLiResponseException not " + cause.getClass().getName());
        RestLiResponseException rlre = (RestLiResponseException) cause;
        Assert.assertEquals(HTTP_CODE, rlre.getStatus());
        Assert.assertEquals(ERR_VALUE, rlre.getErrorDetails().get(ERR_KEY));
        Assert.assertEquals(APP_CODE, rlre.getServiceErrorCode());
        Assert.assertEquals(ERR_MSG, rlre.getServiceErrorMessage());
        // Old
        Assert.assertTrue(cause instanceof RestException, "Expected RestException not " + cause.getClass().getName());
        RestException re = (RestException) cause;
        RestResponse r = re.getResponse();
        ErrorResponse er = new EntityResponseDecoder<ErrorResponse>(ErrorResponse.class).decodeResponse(r).getEntity();
        Assert.assertEquals(HTTP_CODE, r.getStatus());
        Assert.assertEquals(ERR_VALUE, er.getErrorDetails().data().getString(ERR_KEY));
        Assert.assertEquals(APP_CODE, er.getServiceErrorCode().intValue());
        Assert.assertEquals(ERR_MSG, er.getMessage());
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestException(com.linkedin.r2.message.rest.RestException) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RestResponse(com.linkedin.r2.message.rest.RestResponse) ErrorResponse(com.linkedin.restli.common.ErrorResponse) TimeUnit(java.util.concurrent.TimeUnit) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 3 with ErrorResponse

use of com.linkedin.restli.common.ErrorResponse in project parseq by linkedin.

the class GetRequestGroup method unbatchResponse.

private static <K, RT extends RecordTemplate> Response<RT> unbatchResponse(BatchGetEntityRequest<K, RT> request, Response<BatchKVResponse<K, EntityResponse<RT>>> batchResponse, Object id) throws RemoteInvocationException {
    final BatchKVResponse<K, EntityResponse<RT>> batchEntity = batchResponse.getEntity();
    final ErrorResponse errorResponse = batchEntity.getErrors().get(id);
    if (errorResponse != null) {
        throw new RestLiResponseException(errorResponse);
    }
    final EntityResponse<RT> entityResponse = batchEntity.getResults().get(id);
    if (entityResponse != null) {
        final RT entityResult = entityResponse.getEntity();
        if (entityResult != null) {
            return new ResponseImpl<>(batchResponse, entityResult);
        }
    }
    LOGGER.debug("No result or error for base URI : {}, id: {}. Verify that the batchGet endpoint returns response keys that match batchGet request IDs.", request.getBaseUriTemplate(), id);
    throw NOT_FOUND_EXCEPTION;
}
Also used : BatchEntityResponse(com.linkedin.restli.internal.client.response.BatchEntityResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) ResponseImpl(com.linkedin.restli.internal.client.ResponseImpl) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 4 with ErrorResponse

use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.

the class TestBatchKVResponse method testDeserialization.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchKVResponseDataProvider")
public <K> void testDeserialization(List<K> keys, Class<K> keyClass, Class<? extends RecordTemplate> keyKeyClass, Class<? extends RecordTemplate> keyParamsClass, ProtocolVersion protocolVersion) {
    final K resultKey = keys.get(0);
    final K errorKey = keys.get(1);
    final String resultKeyString = URIParamUtils.encodeKeyForBody(resultKey, false, protocolVersion);
    final String errorKeyString = URIParamUtils.encodeKeyForBody(errorKey, false, protocolVersion);
    final DataMap inputResults = new DataMap();
    inputResults.put(resultKeyString, _record.data());
    final DataMap inputErrors = new DataMap();
    inputErrors.put(errorKeyString, _error.data());
    final DataMap testData = new DataMap();
    testData.put(BatchKVResponse.RESULTS, inputResults);
    testData.put(BatchKVResponse.ERRORS, inputErrors);
    final BatchKVResponse<K, TestRecord> response = new BatchKVResponse<>(testData, keyClass, TestRecord.class, Collections.<String, CompoundKey.TypeInfo>emptyMap(), keyKeyClass, keyParamsClass, protocolVersion);
    final Map<K, TestRecord> outputResults = response.getResults();
    final TestRecord outRecord = outputResults.get(resultKey);
    Assert.assertEquals(outRecord, outRecord);
    final Map<K, ErrorResponse> outputErrors = response.getErrors();
    final ErrorResponse outError = outputErrors.get(errorKey);
    Assert.assertEquals(outError, _error);
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey) TestRecord(com.linkedin.restli.client.test.TestRecord) DataMap(com.linkedin.data.DataMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse) Test(org.testng.annotations.Test)

Example 5 with ErrorResponse

use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.

the class TestBatchEntityResponseDecoder method testDecoding.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchEntityResponseDataProvider")
public void testDecoding(List<String> keys, ProtocolVersion protocolVersion) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {
    final String resultKey = keys.get(0);
    final String statusKey = keys.get(1);
    final String errorKey = keys.get(2);
    final DataMap resultData = new DataMap();
    resultData.put(resultKey, _record.data());
    final DataMap statusData = new DataMap();
    statusData.put(statusKey, _status.getCode());
    final DataMap errorData = new DataMap();
    errorData.put(errorKey, _error.data());
    final DataMap data = new DataMap();
    data.put(BatchResponse.RESULTS, resultData);
    data.put(BatchResponse.STATUSES, statusData);
    data.put(BatchResponse.ERRORS, errorData);
    final BatchEntityResponseDecoder<String, TestRecord> decoder = new BatchEntityResponseDecoder<>(new TypeSpec<>(TestRecord.class), new TypeSpec<>(String.class), Collections.<String, CompoundKey.TypeInfo>emptyMap(), null);
    final BatchKVResponse<String, EntityResponse<TestRecord>> response = decoder.wrapResponse(data, Collections.<String, String>emptyMap(), protocolVersion);
    final Map<String, EntityResponse<TestRecord>> results = response.getResults();
    final Map<String, ErrorResponse> errors = response.getErrors();
    final Collection<String> uniqueKeys = new HashSet<>(keys);
    Assert.assertEquals(results.size(), uniqueKeys.size());
    Assert.assertEquals(errors.size(), 1);
    Assert.assertEquals(results.get(resultKey).getEntity(), _record);
    Assert.assertEquals(results.get(statusKey).getStatus(), _status);
    Assert.assertEquals(results.get(errorKey).getError(), _error);
    Assert.assertEquals(errors.get(errorKey), _error);
    // Check that the response still contains the original data map
    Assert.assertEquals(response.data(), data);
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey) DataMap(com.linkedin.data.DataMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) TestRecord(com.linkedin.restli.client.test.TestRecord) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

ErrorResponse (com.linkedin.restli.common.ErrorResponse)67 Test (org.testng.annotations.Test)35 DataMap (com.linkedin.data.DataMap)19 HashMap (java.util.HashMap)15 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)14 RestResponse (com.linkedin.r2.message.rest.RestResponse)9 RestRequest (com.linkedin.r2.message.rest.RestRequest)8 EntityResponse (com.linkedin.restli.common.EntityResponse)8 ResponseImpl (com.linkedin.restli.internal.client.ResponseImpl)8 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)8 RestException (com.linkedin.r2.message.rest.RestException)7 TestRecord (com.linkedin.restli.client.test.TestRecord)7 Callback (com.linkedin.common.callback.Callback)6 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)6 BatchResponse (com.linkedin.restli.common.BatchResponse)6 EmptyRecord (com.linkedin.restli.common.EmptyRecord)6 IOException (java.io.IOException)6 RequestContext (com.linkedin.r2.message.RequestContext)5 BatchCollectionResponse (com.linkedin.restli.common.BatchCollectionResponse)5 BatchFinderCriteriaResult (com.linkedin.restli.common.BatchFinderCriteriaResult)5