Repository: square/okhttp
File: samples/unixdomainsockets/src/main/java/okhttp3/unixdomainsockets/UnixDomainSocketFactory.java (Java)

Code before:
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import javax.net.SocketFactory;
import jnr.unixsocket.UnixSocketChannel;

/** Impersonate TCP-style SocketFactory over UNIX domain sockets. */
public final class UnixDomainSocketFactory extends SocketFactory {
  private final File path;

  public UnixDomainSocketFactory(File path) {
    this.path = path;
  }

  @Override public Socket createSocket() throws IOException {
    UnixSocketChannel channel = UnixSocketChannel.open();
    return new TunnelingUnixSocket(path, channel);
  }

  @Override public Socket createSocket(String host, int port) throws IOException {
    Socket result = createSocket();

    try {
      result.connect(new InetSocketAddress(host, port));
    } catch (IOException e) {
      result.close();
    }
    return result;
  }

  @Override public Socket createSocket(
      String host, int port, InetAddress localHost, int localPort) throws IOException {
    return createSocket(host, port);
  }

  @Override public Socket createSocket(InetAddress host, int port) throws IOException {
    Socket result = createSocket();

    try {
      result.connect(new InetSocketAddress(host, port));
    } catch (IOException e) {
      result.close();
    }
    return result;
  }

  @Override public Socket createSocket(
      InetAddress host, int port, InetAddress localAddress, int localPort) throws IOException {
    return createSocket(host, port);
  }
}

Review comment (suggestion):
This is swallowing the exception and continuing to use it.

Code after:
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import javax.net.SocketFactory;
import jnr.unixsocket.UnixSocketChannel;

/** Impersonate TCP-style SocketFactory over UNIX domain sockets. */
public final class UnixDomainSocketFactory extends SocketFactory {
  private final File path;

  public UnixDomainSocketFactory(File path) {
    this.path = path;
  }

  @Override public Socket createSocket() throws IOException {
    UnixSocketChannel channel = UnixSocketChannel.open();
    return new TunnelingUnixSocket(path, channel);
  }

  @Override public Socket createSocket(String host, int port) throws IOException {
    Socket result = createSocket();

    try {
      result.connect(new InetSocketAddress(host, port));
    } catch (IOException e) {
      result.close();
      throw e;
    }
    return result;
  }

  @Override public Socket createSocket(
      String host, int port, InetAddress localHost, int localPort) throws IOException {
    return createSocket(host, port);
  }

  @Override public Socket createSocket(InetAddress host, int port) throws IOException {
    Socket result = createSocket();

    try {
      result.connect(new InetSocketAddress(host, port));
    } catch (IOException e) {
      result.close();
      throw e;
    }
    return result;
  }

  @Override public Socket createSocket(
      InetAddress host, int port, InetAddress localAddress, int localPort) throws IOException {
    return createSocket(host, port);