Search in sources :

Example 1 with ExceptionFilter

use of net.servicestack.client.ExceptionFilter in project ServiceStack.Java by ServiceStack.

the class TestServiceTests method test_Does_handle_401_Error_with_empty_ResponseBody.

public void test_Does_handle_401_Error_with_empty_ResponseBody() {
    JsonServiceClient testClient = new JsonServiceClient("http://test.servicestack.net");
    // Wow Java, you suck.
    final Exception[] globalError = new Exception[1];
    final Exception[] localError = new Exception[1];
    WebServiceException thrownError = null;
    JsonServiceClient.GlobalExceptionFilter = new ExceptionFilter() {

        @Override
        public void exec(HttpURLConnection res, Exception ex) {
            globalError[0] = ex;
        }
    };
    testClient.ExceptionFilter = new ExceptionFilter() {

        @Override
        public void exec(HttpURLConnection res, Exception ex) {
            localError[0] = ex;
        }
    };
    TestAuth request = new TestAuth();
    try {
        TestAuthResponse response = testClient.send(request);
    } catch (WebServiceException webEx) {
        thrownError = webEx;
    }
    assertNotNull(globalError[0]);
    assertNotNull(localError[0]);
    assertNotNull(thrownError);
    ResponseStatus status = thrownError.getResponseStatus();
    assertNull(status);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) WebServiceException(net.servicestack.client.WebServiceException) ResponseStatus(net.servicestack.client.ResponseStatus) ExceptionFilter(net.servicestack.client.ExceptionFilter) JsonServiceClient(net.servicestack.client.JsonServiceClient) WebServiceException(net.servicestack.client.WebServiceException)

Example 2 with ExceptionFilter

use of net.servicestack.client.ExceptionFilter in project ServiceStack.Java by ServiceStack.

the class TestServiceTests method test_Does_handle_404_Error.

public void test_Does_handle_404_Error() {
    JsonServiceClient testClient = new JsonServiceClient("http://test.servicestack.net");
    // Wow Java, you suck.
    final Exception[] globalError = new Exception[1];
    final Exception[] localError = new Exception[1];
    WebServiceException thrownError = null;
    JsonServiceClient.GlobalExceptionFilter = new ExceptionFilter() {

        @Override
        public void exec(HttpURLConnection res, Exception ex) {
            globalError[0] = ex;
        }
    };
    testClient.ExceptionFilter = new ExceptionFilter() {

        @Override
        public void exec(HttpURLConnection res, Exception ex) {
            localError[0] = ex;
        }
    };
    ThrowType request = new ThrowType().setType("NotFound").setMessage("not here");
    try {
        ThrowTypeResponse response = testClient.put(request);
    } catch (WebServiceException webEx) {
        thrownError = webEx;
    }
    assertNotNull(globalError[0]);
    assertNotNull(localError[0]);
    assertNotNull(thrownError);
    ResponseStatus status = thrownError.getResponseStatus();
    assertEquals("NotFound", status.getErrorCode());
    assertEquals("not here", status.getMessage());
    assertNotNull(status.getStackTrace());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) WebServiceException(net.servicestack.client.WebServiceException) ResponseStatus(net.servicestack.client.ResponseStatus) ExceptionFilter(net.servicestack.client.ExceptionFilter) JsonServiceClient(net.servicestack.client.JsonServiceClient) WebServiceException(net.servicestack.client.WebServiceException)

Example 3 with ExceptionFilter

use of net.servicestack.client.ExceptionFilter in project ServiceStack.Java by ServiceStack.

the class TestServiceTestsAsync method test_Does_handle_404_Error_Async.

public void test_Does_handle_404_Error_Async() throws InterruptedException {
    AndroidServiceClient testClient = new AndroidServiceClient("http://test.servicestack.net");
    // Wow Java, you suck.
    final Exception[] globalError = new Exception[1];
    final Exception[] localError = new Exception[1];
    final WebServiceException[] thrownError = { null };
    AndroidServiceClient.GlobalExceptionFilter = new ExceptionFilter() {

        @Override
        public void exec(HttpURLConnection res, Exception ex) {
            globalError[0] = ex;
        }
    };
    testClient.ExceptionFilter = new ExceptionFilter() {

        @Override
        public void exec(HttpURLConnection res, Exception ex) {
            localError[0] = ex;
        }
    };
    ThrowType request = new ThrowType().setType("NotFound").setMessage("not here");
    final CountDownLatch signal = new CountDownLatch(1);
    testClient.putAsync(request, new AsyncResult<ThrowTypeResponse>() {

        @Override
        public void success(ThrowTypeResponse response) {
            fail("should not succeed");
        }

        @Override
        public void error(Exception ex) {
            thrownError[0] = (WebServiceException) ex;
        }

        @Override
        public void complete() {
            signal.countDown();
        }
    });
    assertTrue(signal.await(5, TimeUnit.SECONDS));
    assertNotNull(globalError[0]);
    assertNotNull(localError[0]);
    assertNotNull(thrownError[0]);
    ResponseStatus status = thrownError[0].getResponseStatus();
    assertEquals("NotFound", status.getErrorCode());
    assertEquals("not here", status.getMessage());
    assertNotNull(status.getStackTrace());
}
Also used : WebServiceException(net.servicestack.client.WebServiceException) ExceptionFilter(net.servicestack.client.ExceptionFilter) CountDownLatch(java.util.concurrent.CountDownLatch) WebServiceException(net.servicestack.client.WebServiceException) HttpURLConnection(java.net.HttpURLConnection) AndroidServiceClient(net.servicestack.android.AndroidServiceClient) ResponseStatus(net.servicestack.client.ResponseStatus)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)3 ExceptionFilter (net.servicestack.client.ExceptionFilter)3 ResponseStatus (net.servicestack.client.ResponseStatus)3 WebServiceException (net.servicestack.client.WebServiceException)3 JsonServiceClient (net.servicestack.client.JsonServiceClient)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 AndroidServiceClient (net.servicestack.android.AndroidServiceClient)1