Search in sources :

Example 1 with RestLiResponseException

use of com.linkedin.restli.internal.server.response.RestLiResponseException in project rest.li by linkedin.

the class TestRestLiServer method testHandleRequestWithRestLiResponseError.

@Test(dataProvider = "restOrStream")
public void testHandleRequestWithRestLiResponseError(final RestOrStream restOrStream) throws Exception {
    final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
    EasyMock.expect(statusResource.get(eq(1L))).andReturn(null).once();
    replay(statusResource);
    Callback<RestLiResponse> restLiResponseCallback = new Callback<RestLiResponse>() {

        @Override
        public void onSuccess(RestLiResponse restLiResponse) {
            fail("We should not get a success here. The server should have returned a 404!");
        }

        @Override
        public void onError(Throwable e) {
            RestLiResponseException restLiResponseException = (RestLiResponseException) e;
            assertEquals(restLiResponseException.getRestLiResponse().getStatus(), HttpStatus.S_404_NOT_FOUND, "We should get a 404 back here!");
            EasyMock.verify(statusResource);
            EasyMock.reset(statusResource);
        }
    };
    if (restOrStream == RestOrStream.REST) {
        RestRequest request = new RestRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
        _server.handleRequestWithRestLiResponse(request, new RequestContext(), restLiResponseCallback);
    } else {
        StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build(EntityStreams.emptyStream());
        _server.handleRequestWithRestLiResponse(streamRequest, new RequestContext(), restLiResponseCallback);
    }
}
Also used : SinglePartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback) Callback(com.linkedin.common.callback.Callback) MultiPartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) AsyncStatusCollectionResource(com.linkedin.restli.server.twitter.AsyncStatusCollectionResource) StatusCollectionResource(com.linkedin.restli.server.twitter.StatusCollectionResource) RestLiResponseException(com.linkedin.restli.internal.server.response.RestLiResponseException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 2 with RestLiResponseException

use of com.linkedin.restli.internal.server.response.RestLiResponseException in project rest.li by linkedin.

the class FilterChainCallbackImpl method onError.

@Override
public void onError(Throwable th, final RestLiResponseData<?> responseData) {
    markOnResponseTimings(_method.getContext().getRawRequestContext());
    // The Throwable passed in is not used at all. However, before every invocation, the throwable is wrapped inside
    // the RestLiResponseData parameter. This can potentially be refactored.
    Throwable error;
    try {
        RestLiServiceException serviceException = responseData.getResponseEnvelope().getException();
        final RestLiResponse response = _responseHandler.buildPartialResponse(_method, responseData);
        error = new RestLiResponseException(serviceException, response);
    } catch (Throwable throwable) {
        LOGGER.error("Unexpected error when processing error response.", responseData.getResponseEnvelope().getException());
        error = throwable;
    }
    _wrappedCallback.onError(error);
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) RestLiResponseException(com.linkedin.restli.internal.server.response.RestLiResponseException)

Example 3 with RestLiResponseException

use of com.linkedin.restli.internal.server.response.RestLiResponseException in project rest.li by linkedin.

the class BaseRestLiServer method buildPreRoutingError.

protected RestLiResponseException buildPreRoutingError(Throwable throwable, Request request) {
    Map<String, String> requestHeaders = request.getHeaders();
    Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, ProtocolVersionUtil.extractProtocolVersion(requestHeaders).toString());
    headers.put(HeaderUtil.getErrorResponseHeaderName(requestHeaders), RestConstants.HEADER_VALUE_ERROR);
    RestLiServiceException restLiServiceException = RestLiServiceException.fromThrowable(throwable);
    ErrorResponse errorResponse = _errorResponseBuilder.buildErrorResponse(restLiServiceException);
    RestLiResponse restLiResponse = new RestLiResponse.Builder().status(restLiServiceException.getStatus()).entity(errorResponse).headers(headers).cookies(Collections.emptyList()).build();
    return new RestLiResponseException(throwable, restLiResponse);
}
Also used : RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) RestLiResponseException(com.linkedin.restli.internal.server.response.RestLiResponseException) TreeMap(java.util.TreeMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 4 with RestLiResponseException

use of com.linkedin.restli.internal.server.response.RestLiResponseException in project rest.li by linkedin.

the class FilterChainCallbackImpl method onResponseSuccess.

@Override
public void onResponseSuccess(final RestLiResponseData<?> responseData) {
    markOnResponseTimings(_method.getContext().getRawRequestContext());
    RestLiResponse partialResponse;
    try {
        partialResponse = _responseHandler.buildPartialResponse(_method, responseData);
    } catch (Throwable th) {
        LOGGER.error("Unexpected error while building the success response. Converting to error response.", th);
        _wrappedCallback.onError(new RestLiResponseException(th, buildErrorResponse(th, responseData)));
        return;
    }
    _wrappedCallback.onSuccess(partialResponse);
}
Also used : RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) RestLiResponseException(com.linkedin.restli.internal.server.response.RestLiResponseException)

Aggregations

RestLiResponse (com.linkedin.restli.internal.server.response.RestLiResponse)4 RestLiResponseException (com.linkedin.restli.internal.server.response.RestLiResponseException)4 Callback (com.linkedin.common.callback.Callback)1 MultiPartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback)1 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)1 RequestContext (com.linkedin.r2.message.RequestContext)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)1 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)1 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)1 ErrorResponse (com.linkedin.restli.common.ErrorResponse)1 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)1 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)1 AsyncStatusCollectionResource (com.linkedin.restli.server.twitter.AsyncStatusCollectionResource)1 StatusCollectionResource (com.linkedin.restli.server.twitter.StatusCollectionResource)1 URI (java.net.URI)1 TreeMap (java.util.TreeMap)1 AfterTest (org.testng.annotations.AfterTest)1 BeforeTest (org.testng.annotations.BeforeTest)1 Test (org.testng.annotations.Test)1