Skip to content

Commit 2186f2d

Browse files
authored
Merge pull request #1509 from nats-io/ncr-exceptions
NatsConnectionReader better exceptions
2 parents 8b6a9ea + 39db57c commit 2186f2d

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/main/java/io/nats/client/impl/NatsConnectionReader.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ enum Mode {
3838
PARSE_PROTO,
3939
GATHER_HEADERS,
4040
GATHER_DATA
41-
};
41+
}
4242

4343
private final NatsConnection connection;
4444

@@ -240,8 +240,9 @@ void gatherOp(int maxPos) throws IOException {
240240
this.opPos++;
241241
}
242242
}
243-
} catch (ArrayIndexOutOfBoundsException | IllegalStateException | NumberFormatException | NullPointerException ex) {
244-
this.encounteredProtocolError(ex);
243+
}
244+
catch (ArrayIndexOutOfBoundsException | IllegalStateException | NumberFormatException | NullPointerException ex) {
245+
throw new IOException("Gather Operation", ex);
245246
}
246247
}
247248

@@ -270,8 +271,9 @@ void gatherMessageProtocol(int maxPos) throws IOException {
270271
this.msgLinePosition++;
271272
}
272273
}
273-
} catch (IllegalStateException | NumberFormatException | NullPointerException ex) {
274-
this.encounteredProtocolError(ex);
274+
}
275+
catch (IllegalStateException | NumberFormatException | NullPointerException ex) {
276+
throw new IOException("Gather Message", ex);
275277
}
276278
}
277279

@@ -301,8 +303,9 @@ void gatherProtocol(int maxPos) throws IOException {
301303
this.protocolBuffer.put(b);
302304
}
303305
}
304-
} catch (IllegalStateException | NumberFormatException | NullPointerException ex) {
305-
this.encounteredProtocolError(ex);
306+
}
307+
catch (IllegalStateException | NumberFormatException | NullPointerException ex) {
308+
throw new IOException("Gather Protocol", ex);
306309
}
307310
}
308311

@@ -335,8 +338,9 @@ void gatherHeaders(int maxPos) throws IOException {
335338
throw new IllegalStateException("Bad socket data, headers do not match expected length");
336339
}
337340
}
338-
} catch (IllegalStateException | NullPointerException ex) {
339-
this.encounteredProtocolError(ex);
341+
}
342+
catch (IllegalStateException | NullPointerException ex) {
343+
throw new IOException("Gather Header", ex);
340344
}
341345
}
342346

@@ -386,8 +390,9 @@ void gatherMessageData(int maxPos) throws IOException {
386390
throw new IllegalStateException("Bad socket data, no CRLF after data");
387391
}
388392
}
389-
} catch (IllegalStateException | NullPointerException ex) {
390-
this.encounteredProtocolError(ex);
393+
}
394+
catch (IllegalStateException | NullPointerException ex) {
395+
throw new IOException("Gather Message Data", ex);
391396
}
392397
}
393398

@@ -500,7 +505,7 @@ void parseProtocolMessage() throws IOException {
500505
String subject = grabNextMessageLineElement(protocolLength);
501506
String sid = grabNextMessageLineElement(protocolLength);
502507
String replyTo = grabNextMessageLineElement(protocolLength);
503-
String lengthChars = null;
508+
String lengthChars;
504509

505510
if (this.msgLinePosition < protocolLength) {
506511
lengthChars = grabNextMessageLineElement(protocolLength);
@@ -540,15 +545,16 @@ void parseProtocolMessage() throws IOException {
540545
String hdrLenOrTotLen = grabNextMessageLineElement(hProtocolLength);
541546

542547
String hReplyTo = null;
543-
int hdrLen = -1;
544-
int totLen = -1;
548+
int hdrLen;
549+
int totLen;
545550

546551
// if there is more it must be replyTo hdrLen totLen instead of just hdrLen totLen
547552
if (this.msgLinePosition < hProtocolLength) {
548553
hReplyTo = replyToOrHdrLen;
549554
hdrLen = parseLength(hdrLenOrTotLen);
550555
totLen = parseLength(grabNextMessageLineElement(hProtocolLength));
551-
} else {
556+
}
557+
else {
552558
hdrLen = parseLength(replyToOrHdrLen);
553559
totLen = parseLength(hdrLenOrTotLen);
554560
}
@@ -600,14 +606,10 @@ void parseProtocolMessage() throws IOException {
600606
default:
601607
throw new IllegalStateException("Unknown protocol operation "+op);
602608
}
603-
604-
} catch (IllegalStateException | NumberFormatException | NullPointerException ex) {
605-
this.encounteredProtocolError(ex);
606609
}
607-
}
608-
609-
void encounteredProtocolError(Exception ex) throws IOException {
610-
throw new IOException(ex);
610+
catch (IllegalStateException | NumberFormatException | NullPointerException ex) {
611+
throw new IOException("Parse Protocol OP_" + op, ex);
612+
}
611613
}
612614

613615
//For testing

0 commit comments

Comments
 (0)