nats.java icon indicating copy to clipboard operation
nats.java copied to clipboard

Generic exception is thrown when publishing a JetStream message

Open joschiwald opened this issue 4 years ago • 7 comments

Better identify JetStream connection and /communication errors.

Versions of io.nats:jnats and nats-server:

io.nats:jnats: 2.16.1 nats-server: 2.8.4

OS/Container environment:

Client: Windows 10 Server: alpine docker image

Steps or code to reproduce the issue:

  1. Start a nats-server with default configuration and no streams configured
  2. Publish a JetStream message
natsConn.jetStream().publish("test", myMessageBytes)

Expected result:

The publish function throws a JetStreamApiException with the right error code.

Actual result:

The publish function throws an IOException https://github.com/nats-io/nats.java/blob/main/src/main/java/io/nats/client/impl/NatsJetStream.java#L153

joschiwald avatar Oct 13 '22 09:10 joschiwald

For now you need to look at the getMessage() that came back in the IOException

Error Publishing: 503 No Responders Available For Request

Changing this would be a breaking change since JetStreamApiException is not an IOException

I'm changing this issue to an enhancement instead of a bug.

scottf avatar Oct 19 '22 18:10 scottf

This is my first approach to solve this issue. I'll test it next week when I have access to my development environment.

diff --git a/src/main/java/io/nats/client/api/ApiResponse.java b/src/main/java/io/nats/client/api/ApiResponse.java
index 5257cb4c..2353759f 100644
--- a/src/main/java/io/nats/client/api/ApiResponse.java
+++ b/src/main/java/io/nats/client/api/ApiResponse.java
@@ -31,7 +31,15 @@ public abstract class ApiResponse<T> {
     private final Error error;
 
     public ApiResponse(Message msg) {
-        this(new String(msg.getData(), UTF_8));
+        if (msg.isStatusMessage()) {
+            json = null;
+            error = Error.convert(msg.getStatus());
+            type = NO_TYPE;
+        } else {
+            json = new String(msg.getData(), UTF_8);
+            error = json == null ? null : Error.optionalInstance(json);
+            type = json == null ? NO_TYPE : JsonUtils.readString(json, TYPE_RE, NO_TYPE);
+        }
     }
 
     public ApiResponse(String json) {
diff --git a/src/main/java/io/nats/client/impl/NatsJetStream.java b/src/main/java/io/nats/client/impl/NatsJetStream.java
index d0145571..8e339cce 100644
--- a/src/main/java/io/nats/client/impl/NatsJetStream.java
+++ b/src/main/java/io/nats/client/impl/NatsJetStream.java
@@ -149,10 +149,6 @@ public class NatsJetStream extends NatsJetStreamImplBase implements JetStream {
     }
 
     private PublishAck processPublishResponse(Message resp, PublishOptions options) throws IOException, JetStreamApiException {
-        if (resp.isStatusMessage()) {
-            throw new IOException("Error Publishing: " + resp.getStatus().getCode() + " " + resp.getStatus().getMessage());
-        }
-
         PublishAck ack = new PublishAck(resp);
         String ackStream = ack.getStream();
         String pubStream = options == null ? null : options.getStream();

joschiwald avatar Oct 22 '22 12:10 joschiwald

Yes, something like that would work, but again, we cannot change the type of exception thrown without a major version release as it would be a breaking change.

scottf avatar Oct 24 '22 12:10 scottf

Also see https://github.com/nats-io/nats.java/issues/858

scottf avatar Mar 18 '23 11:03 scottf

@joschiwald I'm thinking about how to fix this in a backward compatible manner. I could add a connection option that is essentially "better error messages" and behave differently if the user sets this option. What do you think?

scottf avatar Apr 11 '23 20:04 scottf

I like the idea. With that option we could remove the workaround with exception mapping from our code 😊I hope this is doable without having to add to much bloat to your codebase.

joschiwald avatar Apr 16 '23 13:04 joschiwald

This is better supported due to this PR https://github.com/nats-io/nats.java/pull/1582

scottf avatar Jun 17 '26 14:06 scottf