Skip to content

Commit

Permalink
Merge pull request #39 from igrishaev/io-exception-type
Browse files Browse the repository at this point in the history
Introduce error hierarchy and PGErrorIO
  • Loading branch information
igrishaev authored Feb 6, 2025
2 parents 79afc4d + a5e6442 commit d403c82
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## 0.1.34-SNAPSHOT

- ?
- #39 Introduce error hierarchy and PGErrorIO @jgdavey

## 0.1.33

Expand Down
14 changes: 10 additions & 4 deletions docs/errors.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Errors and Exceptions

PG2 introduces two kinds of exceptions: `PGError` and `PGErrorResponse`.
PG2 introduces 3 kinds of exceptions: `PGError`, `PGErrorResponse`, and
`PGErrorIO`. `PGError` is the basic one, and both `PGErrorResponse` and
`PGErrorIO` are inherited from it.

**A `PGError` exception** usually happens on the client side due to wrong values
or encode/decode errors. Below, the exception pops up because PG2 cannot encode
Expand All @@ -20,9 +22,13 @@ an `Object` instance into an integer:
didn't like what you sent. It might be an syntax issue, wrong parameters, or
whatever else.

The `ErrorResponse` message carries plenty of fields describing an error, and
the `PGErrorResponse` class shares them. Namely, the error message renders all
the fields received from the server:
**A `PGErrorIO`** exception usually occurs based on connection-related
issues, for example being unable to connect to the server, or being unable to
read/write to the connection.

The standard Postgres `ErrorResponse` message carries plenty of fields
describing an error, and the `PGErrorResponse` class shares them. Namely, the
error message renders all the fields received from the server:

~~~clojure
(pg/execute conn "selekt 1")
Expand Down
2 changes: 1 addition & 1 deletion pg-core/src/java/org/pg/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private void setInputStream(final InputStream in) {

private void setOutputStream(final OutputStream out) {
final int len = config.outStreamBufSize();
outStream = new BufferedOutputStream(out, len);
outStream = IOTool.wrapBuf(out, len);
}

private SocketChannel connectSocket(final SocketAddress address) {
Expand Down
2 changes: 1 addition & 1 deletion pg-core/src/java/org/pg/error/PGError.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.pg.error;

public final class PGError extends RuntimeException {
public class PGError extends RuntimeException {

public PGError (final String message) {
super(message);
Expand Down
17 changes: 17 additions & 0 deletions pg-core/src/java/org/pg/error/PGErrorIO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.pg.error;

public final class PGErrorIO extends PGError {

@SuppressWarnings("unused")
public PGErrorIO(final String message) {
super(message);
}

public PGErrorIO(final Throwable e, final String message) {
super(e, message);
}

public PGErrorIO(final Throwable e, final String template, final Object... args) {
super(e, String.format(template, args));
}
}
2 changes: 1 addition & 1 deletion pg-core/src/java/org/pg/error/PGErrorResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import java.util.Map;

public final class PGErrorResponse extends RuntimeException implements IExceptionInfo {
public final class PGErrorResponse extends PGError implements IExceptionInfo {

private final ErrorResponse errorResponse;
private final String sql;
Expand Down
26 changes: 13 additions & 13 deletions pg-core/src/java/org/pg/util/IOTool.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.pg.util;

import org.pg.error.PGError;
import org.pg.error.PGErrorIO;

import java.io.*;
import java.net.Socket;
Expand All @@ -11,15 +11,15 @@ public static void close (final InputStream inputStream) {
try {
inputStream.close();
} catch (IOException e) {
throw new PGError(e, "cannot close input stream, cause: %s", e.getMessage());
throw new PGErrorIO(e, "cannot close input stream, cause: %s", e.getMessage());
}
}

public static void close (final OutputStream outputStream) {
try {
outputStream.close();
} catch (IOException e) {
throw new PGError(e, "cannot close output stream, cause: %s", e.getMessage());
throw new PGErrorIO(e, "cannot close output stream, cause: %s", e.getMessage());
}
}

Expand All @@ -28,7 +28,7 @@ public static void skip (final InputStream inputStream, final int len) {
inputStream.readNBytes(len);
}
catch (IOException e) {
throw new PGError("Could not skip %s byte(s), cause: %s", len, e.getMessage());
throw new PGErrorIO(e, "Could not skip %s byte(s), cause: %s", len, e.getMessage());
}
}

Expand All @@ -37,7 +37,7 @@ public static byte[] readNBytes (final InputStream inputStream, final int len) {
return inputStream.readNBytes(len);
}
catch (IOException e) {
throw new PGError("Could not read %s byte(s), cause: %s", len, e.getMessage());
throw new PGErrorIO(e, "Could not read %s byte(s), cause: %s", len, e.getMessage());
}
}

Expand All @@ -48,7 +48,7 @@ public static int read (
try {
return inputStream.read(buf);
} catch (IOException e) {
throw new PGError(e, "cannot read from the input stream");
throw new PGErrorIO(e, "cannot read from the input stream");
}
}

Expand All @@ -61,7 +61,7 @@ public static int read (
try {
return inputStream.read(buf, offset, len);
} catch (IOException e) {
throw new PGError(e, "cannot read from the input stream");
throw new PGErrorIO(e, "cannot read from the input stream");
}
}

Expand All @@ -71,7 +71,7 @@ public static int read (
try {
return inputStream.read();
} catch (IOException e) {
throw new PGError(e, "cannot read from the input stream");
throw new PGErrorIO(e, "cannot read from the input stream");
}
}

Expand All @@ -95,15 +95,15 @@ public static void write(final OutputStream outputStream, final byte[] buf) {
try {
outputStream.write(buf);
} catch (IOException e) {
throw new PGError(e, "cannot write a byte array into an output stream");
throw new PGErrorIO(e, "cannot write a byte array into an output stream");
}
}

public static void flush(final OutputStream outputStream) {
try {
outputStream.flush();
} catch (IOException e) {
throw new PGError(e, "cannot flush an output stream");
throw new PGErrorIO(e, "cannot flush an output stream");
}
}

Expand All @@ -116,7 +116,7 @@ public static void write (
try {
outputStream.write(buf, offset, len);
} catch (IOException e) {
throw new PGError(
throw new PGErrorIO(
e,
"cannot write a byte array into an output stream, offset: %s, len: %s",
offset, len
Expand All @@ -129,7 +129,7 @@ public static InputStream getInputStream(final Socket socket) {
return socket.getInputStream();
}
catch (IOException e) {
throw new PGError(
throw new PGErrorIO(
e,
"cannot get an input stream from a socket"
);
Expand All @@ -141,7 +141,7 @@ public static OutputStream getOutputStream(final Socket socket) {
return socket.getOutputStream();
}
catch (IOException e) {
throw new PGError(
throw new PGErrorIO(
e,
"cannot get an output stream from a socket"
);
Expand Down
8 changes: 4 additions & 4 deletions pg-core/src/java/org/pg/util/SocketTool.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.pg.util;

import org.pg.error.PGError;
import org.pg.error.PGErrorIO;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
Expand All @@ -15,15 +15,15 @@ public static void startHandshake(final SSLSocket socket) {
try {
socket.startHandshake();
} catch (IOException e) {
throw new PGError(e, "cannot start handshake, cause: %s", e.getMessage());
throw new PGErrorIO(e, "cannot start handshake, cause: %s", e.getMessage());
}
}

public static SocketChannel open(final SocketAddress address) {
try {
return SocketChannel.open(address);
} catch (IOException e) {
throw new PGError(e, "cannot open socket, address: %s, cause: %s", address, e.getMessage());
throw new PGErrorIO(e, "cannot open socket, address: %s, cause: %s", address, e.getMessage());
}
}

Expand All @@ -35,7 +35,7 @@ public static SSLSocket open(final SSLContext sslContext,
try {
return (SSLSocket) sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
} catch (IOException e) {
throw new PGError(e, "cannot open an SSL socket, socket: %s, host: %s, port: %s, cause: %s",
throw new PGErrorIO(e, "cannot open an SSL socket, socket: %s, host: %s, port: %s, cause: %s",
socket, host, port, e.getMessage());
}
}
Expand Down

0 comments on commit d403c82

Please sign in to comment.