Generic exception is thrown when publishing a JetStream message
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:
- Start a nats-server with default configuration and no streams configured
- 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
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.
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();
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.
Also see https://github.com/nats-io/nats.java/issues/858
@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?
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.
This is better supported due to this PR https://github.com/nats-io/nats.java/pull/1582