mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.57k stars 1.07k forks source link

MockServerClient connection exception handling #85

Closed DominicCliftonNEKTAN closed 9 years ago

DominicCliftonNEKTAN commented 9 years ago

When the java MockServerClient cannot connect to the mock server, in our case started by node, an exception is thrown. The type of exception is RuntimeException which has a cause of ConnectException.

In order to retry the operation we currently have to catch any exceptions in a catch block and then check the exception's cause before decided wether to retry or fail. This is bad as our code is now coupled to internal behaviours of the mock server client.

Can the mock server client be updated so that it throws a more appropriate exception, e.g. MockServerCommunicationException or something of that ilk which we can then use?

example code:


    private void createMockServerClient() {
        int attemptsRemaining = 5;

        while (attemptsRemaining) {
            try {
                mockServerClient = new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
                mockServerClient.reset()
                break;
            } catch (Exception caught) {
                if (!(caught.cause instanceof ConnectException)) {
                    throw caught
                }
                attemptsRemaining--;
                if (!attemptsRemaining) {
                    throw caught
                }
                println "Retrying connection to mock server, attempts remaining: ${attemptsRemaining}"
                Thread.sleep(250)
            }
        }
    }

I'd rather this was something like:


    private void createMockServerClient() {
        int attemptsRemaining = 5;

        while (attemptsRemaining) {
            try {
                mockServerClient = new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
                mockServerClient.reset()
                break;
            } catch (MockServerConnectionException caught) {
                attemptsRemaining--;
                if (!attemptsRemaining) {
                    throw caught
                }
                println "Retrying connection to mock server, attempts remaining: ${attemptsRemaining}"
                Thread.sleep(250)
            }
        }
    }
jamesdbloom commented 9 years ago

Fixed