Search in sources :

Example 1 with WebServiceException

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

the class JsonServiceClientTests method test_does_process_missing_service_correctly.

public void test_does_process_missing_service_correctly() {
    JsonServiceClient localTestClient = new JsonServiceClient("http://techstacks.io/");
    try {
        localTestClient.get(new EchoTypes());
        fail("Should throw");
    } catch (WebServiceException ex) {
        assertEquals(ex.getStatusCode(), 405);
    }
}
Also used : WebServiceException(net.servicestack.client.WebServiceException) JsonServiceClient(net.servicestack.client.JsonServiceClient)

Example 2 with WebServiceException

use of net.servicestack.client.WebServiceException 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 3 with WebServiceException

use of net.servicestack.client.WebServiceException 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 4 with WebServiceException

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

the class TestServiceTestsAsync method test_Does_handle_ValidationException_Async.

public void test_Does_handle_ValidationException_Async() {
    ThrowValidation request = new ThrowValidation().setEmail("invalidemail");
    final CountDownLatch signal = new CountDownLatch(1);
    client.postAsync(request, new AsyncResult<ThrowValidationResponse>() {

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

        @Override
        public void error(Exception ex) {
            WebServiceException webEx = (WebServiceException) ex;
            ResponseStatus status = webEx.getResponseStatus();
            assertNotNull(status);
            assertEquals(3, status.getErrors().size());
            assertEquals(status.getErrors().get(0).getErrorCode(), status.getErrorCode());
            assertEquals(status.getErrors().get(0).getMessage(), status.getMessage());
            assertEquals("InclusiveBetween", status.getErrors().get(0).errorCode);
            assertEquals("'Age' must be between 1 and 120. You entered 0.", status.getErrors().get(0).getMessage());
            assertEquals("Age", status.getErrors().get(0).getFieldName());
            assertEquals("NotEmpty", status.getErrors().get(1).errorCode);
            assertEquals("'Required' should not be empty.", status.getErrors().get(1).getMessage());
            assertEquals("Required", status.getErrors().get(1).getFieldName());
            assertEquals("Email", status.getErrors().get(2).errorCode);
            assertEquals("'Email' is not a valid email address.", status.getErrors().get(2).getMessage());
            assertEquals("Email", status.getErrors().get(2).getFieldName());
        }

        @Override
        public void complete() {
            signal.countDown();
        }
    });
}
Also used : WebServiceException(net.servicestack.client.WebServiceException) ResponseStatus(net.servicestack.client.ResponseStatus) CountDownLatch(java.util.concurrent.CountDownLatch) WebServiceException(net.servicestack.client.WebServiceException)

Example 5 with WebServiceException

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

the class TechStacksServiceTests method test_does_handle_auth_failure.

public void test_does_handle_auth_failure() {
    JsonServiceClient techStacksClient = new JsonServiceClient("http://techstacks.io/");
    int errorCode = 0;
    try {
        LockTechStack request = new LockTechStack();
        request.setTechnologyStackId((long) 6);
        LockStackResponse res = techStacksClient.post(request);
        fail("Should throw");
    } catch (WebServiceException ex) {
        // private StatusCode has correct code, response status is null due to empty response body.
        errorCode = ex.getStatusCode();
    }
    assertEquals(errorCode, 401);
}
Also used : WebServiceException(net.servicestack.client.WebServiceException) JsonServiceClient(net.servicestack.client.JsonServiceClient)

Aggregations

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