Search in sources :

Example 16 with ErrorResponse

use of com.linkedin.restli.common.ErrorResponse 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 17 with ErrorResponse

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

the class RestClientTest method testEmptyErrorResponse.

@Test
public void testEmptyErrorResponse() {
    RestResponse response = new RestResponseBuilder().setStatus(200).build();
    RestLiResponseException e = new RestLiResponseException(response, null, new ErrorResponse());
    Assert.assertNull(e.getServiceErrorMessage());
    Assert.assertNull(e.getErrorDetails());
    Assert.assertNull(e.getErrorSource());
    Assert.assertFalse(e.hasServiceErrorCode());
    Assert.assertNull(e.getServiceErrorStackTrace());
    Assert.assertNull(e.getServiceExceptionClass());
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) ErrorResponse(com.linkedin.restli.common.ErrorResponse) Test(org.testng.annotations.Test)

Example 18 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 19 with ErrorResponse

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

the class RestLiInternalException method toErrorResponse.

public ErrorResponse toErrorResponse() {
    ErrorResponse response = new ErrorResponse();
    response.setStatus(RestStatus.INTERNAL_SERVER_ERROR);
    response.setExceptionClass(RestLiInternalException.class.getName());
    if (getMessage() != null) {
        response.setMessage(getMessage());
    }
    return response;
}
Also used : ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 20 with ErrorResponse

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

the class TestBatchGetResponseBuilder method testBuilder.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "testData")
@SuppressWarnings("unchecked")
public void testBuilder(Object results, ProtocolVersion protocolVersion, Map<String, Foo> expectedTransformedResult, Map<String, ErrorResponse> expectedErrors, Map<Object, RestLiServiceException> expectedExceptionsWithUntypedKey, MaskTree maskTree, ProjectionMode projectionMode) {
    ResourceContext mockContext = getMockResourceContext(protocolVersion, expectedExceptionsWithUntypedKey, null, maskTree, projectionMode);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    BatchGetResponseBuilder responseBuilder = new BatchGetResponseBuilder(new ErrorResponseBuilder());
    RestLiResponseData responseData = responseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.<HttpCookie>emptyList());
    PartialRestResponse restResponse = responseBuilder.buildResponse(routingResult, responseData);
    EasyMock.verify(mockContext, mockDescriptor);
    ResponseBuilderUtil.validateHeaders(restResponse, headers);
    Assert.assertEquals(restResponse.getStatus(), HttpStatus.S_200_OK);
    BatchResponse<Foo> entity = (BatchResponse<Foo>) restResponse.getEntity();
    Assert.assertEquals(entity.getResults(), expectedTransformedResult);
    if (results instanceof BatchResult) {
        Map<String, Integer> expectedStatuses = new HashMap<String, Integer>();
        for (String key : entity.getResults().keySet()) {
            expectedStatuses.put(key, HttpStatus.S_200_OK.getCode());
        }
        Assert.assertEquals(entity.getStatuses(), expectedStatuses);
    } else {
        // if the resource returns a Map we don't have a separate status map in the BatchResponse
        Assert.assertEquals(entity.getStatuses(), Collections.emptyMap());
    }
    Assert.assertEquals(entity.getErrors().size(), expectedErrors.size());
    for (Map.Entry<String, ErrorResponse> entry : entity.getErrors().entrySet()) {
        String key = entry.getKey();
        ErrorResponse value = entry.getValue();
        Assert.assertEquals(value.getStatus(), expectedErrors.get(key).getStatus());
    }
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BatchResponse(com.linkedin.restli.common.BatchResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Foo(com.linkedin.pegasus.generator.examples.Foo) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) BatchResult(com.linkedin.restli.server.BatchResult) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.testng.annotations.Test)

Aggregations

ErrorResponse (com.linkedin.restli.common.ErrorResponse)41 Test (org.testng.annotations.Test)20 DataMap (com.linkedin.data.DataMap)12 HashMap (java.util.HashMap)12 RestResponse (com.linkedin.r2.message.rest.RestResponse)8 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)8 RestException (com.linkedin.r2.message.rest.RestException)6 RestRequest (com.linkedin.r2.message.rest.RestRequest)6 IOException (java.io.IOException)6 CompoundKey (com.linkedin.restli.common.CompoundKey)5 Map (java.util.Map)5 Callback (com.linkedin.common.callback.Callback)4 MultiPartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback)4 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)4 RequestContext (com.linkedin.r2.message.RequestContext)4 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)4 StreamException (com.linkedin.r2.message.stream.StreamException)4 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)4 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)4 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)4