Skip to content

Commit 3e48f98

Browse files
committed
Minor improvements to error reporting
1 parent 887722d commit 3e48f98

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,7 +1706,9 @@ public byte[] getBinaryValue(Base64Variant b64variant) throws IOException
17061706
} else if (_currToken == JsonToken.VALUE_STRING) {
17071707
return _getBinaryFromString(b64variant);
17081708
} else {
1709-
_reportError("Current token ("+currentToken()+") not VALUE_EMBEDDED_OBJECT, can not access as binary");
1709+
throw _constructReadException(
1710+
"Current token (%s) not VALUE_EMBEDDED_OBJECT or VALUE_STRING, can not access as binary",
1711+
currentToken());
17101712
}
17111713
return _binaryValue;
17121714
}
@@ -1735,7 +1737,9 @@ public int readBinaryValue(Base64Variant b64variant, OutputStream out) throws IO
17351737
out.write(b, 0, len);
17361738
return len;
17371739
}
1738-
_reportError("Current token ("+currentToken()+") not VALUE_EMBEDDED_OBJECT, can not access as binary");
1740+
throw _constructReadException(
1741+
"Current token (%s) not VALUE_EMBEDDED_OBJECT or VALUE_STRING, can not access as binary",
1742+
currentToken());
17391743
}
17401744
if (!_tokenIncomplete) { // someone already decoded or read
17411745
if (_binaryValue == null) { // if this method called twice in a row
@@ -2774,7 +2778,8 @@ private final String _decodeContiguousName(int len) throws IOException
27742778
i = 0xDC00 | (i & 0x3FF);
27752779
break;
27762780
default: // invalid
2777-
_reportError("Invalid UTF-8 byte 0x"+Integer.toHexString(i)+" in Object property name");
2781+
throw _constructReadException("Invalid UTF-8 byte 0x%s in Object property name",
2782+
Integer.toHexString(i));
27782783
}
27792784
}
27802785
outBuf[outPtr++] = (char) i;
@@ -3067,7 +3072,9 @@ protected void _skipChunked(int expectedType) throws IOException
30673072
_skipBytesL(_decode64Bits());
30683073
break;
30693074
case 31:
3070-
throw _constructError("Illegal chunked-length indicator within chunked-length value (type "+expectedType+")");
3075+
throw _constructReadException(
3076+
"Illegal chunked-length indicator within chunked-length value (type %d)",
3077+
expectedType);
30713078
default:
30723079
_invalidToken(_typeByte);
30733080
}
@@ -3119,11 +3126,12 @@ private final int _decodeTag(int lowBits) throws IOException
31193126
// only allow for cases where encoder is being wasteful...
31203127
long l = _decode64Bits();
31213128
if (l < MIN_INT_L || l > MAX_INT_L) {
3122-
_reportError("Illegal Tag value: "+l);
3129+
throw _constructReadException("Illegal Tag value: %d", l);
31233130
}
31243131
return (int) l;
31253132
}
3126-
throw _constructError("Invalid low bits for Tag token: 0x"+Integer.toHexString(lowBits));
3133+
throw _constructReadException("Invalid low bits for Tag token: 0x%s",
3134+
Integer.toHexString(lowBits));
31273135
}
31283136

31293137
/**

smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileParser.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,8 @@ public JsonToken nextToken() throws IOException
536536
// should we change description based on reserved vs illegal? (special cases
537537
// of null bytes etc)
538538
private JsonToken _reportUnknownValueTypeToken(int ch) throws IOException {
539-
_reportError("Invalid type marker byte 0x"+Integer.toHexString(ch & 0xFF)+" for expected value token");
540-
return null;
539+
throw _constructReadException("Invalid type marker byte 0x%s for expected value token",
540+
Integer.toHexString(ch & 0xFF));
541541
}
542542

543543
// Helper method called in situations where Smile Header was encountered
@@ -877,8 +877,8 @@ public String nextFieldName() throws IOException
877877
// Should we try to give more information on kind of problem (reserved vs
878878
// illegal by definition; suggesting likely problem)
879879
private String _reportUnknownNameToken(int ch) throws IOException {
880-
_reportError("Invalid type marker byte 0x"+Integer.toHexString(ch & 0xFF)+" for expected field name (or END_OBJECT marker)");
881-
return null;
880+
throw _constructReadException("Invalid type marker byte 0x%s for expected field name (or END_OBJECT marker)",
881+
Integer.toHexString(ch & 0xFF));
882882
}
883883

884884
@Override
@@ -1211,7 +1211,9 @@ public byte[] getBinaryValue(Base64Variant b64variant) throws IOException
12111211
} else if (_currToken == JsonToken.VALUE_STRING) {
12121212
return _getBinaryFromString(b64variant);
12131213
} else {
1214-
_reportError("Current token ("+currentToken()+") not VALUE_EMBEDDED_OBJECT, can not access as binary");
1214+
throw _constructReadException(
1215+
"Current token (%s) not VALUE_EMBEDDED_OBJECT or VALUE_STRING, can not access as binary",
1216+
currentToken());
12151217
}
12161218
return _binaryValue;
12171219
}
@@ -1240,7 +1242,9 @@ public int readBinaryValue(Base64Variant b64variant, OutputStream out) throws IO
12401242
out.write(b, 0, len);
12411243
return len;
12421244
}
1243-
_reportError("Current token ("+currentToken()+") not VALUE_EMBEDDED_OBJECT, can not access as binary");
1245+
throw _constructReadException(
1246+
"Current token (%s) not VALUE_EMBEDDED_OBJECT or VALUE_STRING, can not access as binary",
1247+
currentToken());
12441248
}
12451249
// Ok, first, unlikely (but legal?) case where someone already requested binary data:
12461250
if (!_tokenIncomplete) {
@@ -1448,8 +1452,8 @@ protected final JsonToken _handleFieldName() throws IOException
14481452
break;
14491453
}
14501454
// Other byte values are illegal
1451-
_reportError("Invalid type marker byte 0x"+Integer.toHexString(_typeAsInt)+" for expected field name (or END_OBJECT marker)");
1452-
return null;
1455+
throw _constructReadException("Invalid type marker byte 0x%s for expected field name (or END_OBJECT marker)",
1456+
Integer.toHexString(_typeAsInt));
14531457
}
14541458

14551459
private String _findOrDecodeShortAsciiName(final int len) throws IOException
@@ -1609,7 +1613,8 @@ private final String _decodeShortUnicodeName(int len)
16091613
default: // invalid
16101614
// Update pointer here to point to (more) correct location
16111615
_inputPtr = inPtr;
1612-
_reportError(String.format("Invalid byte 0x%02X in short Unicode text block", i));
1616+
throw _constructReadException(
1617+
"Invalid byte 0x%02X in short Unicode text block", i);
16131618
}
16141619
}
16151620
outBuf[outPtr++] = (char) i;
@@ -2454,7 +2459,7 @@ protected final String _decodeShortUnicodeValue(final int byteLen) throws IOExce
24542459
i = 0xDC00 | (i & 0x3FF);
24552460
break;
24562461
default: // invalid
2457-
_reportError(String.format("Invalid byte 0x%02X in short Unicode text block", i));
2462+
throw _constructReadException("Invalid byte 0x%02X in short Unicode text block", i);
24582463
}
24592464
outBuf[outPtr++] = (char) i;
24602465
}
@@ -3060,7 +3065,7 @@ protected String _reportTruncatedUTF8InString(int strLenBytes, int truncatedChar
30603065
int firstUTFByteValue, int bytesExpected)
30613066
throws IOException
30623067
{
3063-
throw _constructError(String.format(
3068+
throw _constructReadException(String.format(
30643069
"Truncated UTF-8 character in Short Unicode String value (%d bytes): "
30653070
+"byte 0x%02X at offset #%d indicated %d more bytes needed",
30663071
strLenBytes, firstUTFByteValue, truncatedCharOffset, bytesExpected));

0 commit comments

Comments
 (0)