#78 improve exception handling

This commit is contained in:
Yann Massard 2020-04-29 09:15:57 +02:00 committed by David Renshaw
parent 37d067797a
commit a16a8b517c
8 changed files with 29 additions and 40 deletions

View file

@ -100,7 +100,7 @@ public final class BufferedInputStreamWrapper implements BufferedInputStream {
while (numRead < minBytes) { while (numRead < minBytes) {
int res = reader.read(buf); int res = reader.read(buf);
if (res < 0) { if (res < 0) {
throw new Error("premature EOF"); throw new DecodeException("premature EOF");
} }
numRead += res; numRead += res;
} }

View file

@ -41,7 +41,7 @@ public final class ElementSize {
case EIGHT_BYTES: return 64; case EIGHT_BYTES: return 64;
case POINTER: return 0; case POINTER: return 0;
case INLINE_COMPOSITE: return 0; case INLINE_COMPOSITE: return 0;
default : throw new Error("impossible field size: " + size); default : throw new IllegalArgumentException("impossible field size: " + size);
} }
} }

View file

@ -28,7 +28,7 @@ public final class GeneratedClassSupport {
buffer.order(java.nio.ByteOrder.LITTLE_ENDIAN); buffer.order(java.nio.ByteOrder.LITTLE_ENDIAN);
return new SegmentReader(buffer, new ReaderArena(new java.nio.ByteBuffer[0], 0x7fffffffffffffffL)); return new SegmentReader(buffer, new ReaderArena(new java.nio.ByteBuffer[0], 0x7fffffffffffffffL));
} catch (Exception e) { } catch (Exception e) {
throw new Error("could not decode raw bytes from String"); throw new DecodeException("could not decode raw bytes from String");
} }
} }
} }

View file

@ -80,10 +80,10 @@ public final class MessageBuilder {
if (rootSegment.currentSize() == 0) { if (rootSegment.currentSize() == 0) {
int location = rootSegment.allocate(1); int location = rootSegment.allocate(1);
if (location == SegmentBuilder.FAILED_ALLOCATION) { if (location == SegmentBuilder.FAILED_ALLOCATION) {
throw new Error("could not allocate root pointer"); throw new RuntimeException("could not allocate root pointer");
} }
if (location != 0) { if (location != 0) {
throw new Error("First allocated word of new segment was not at offset 0"); throw new RuntimeException("First allocated word of new segment was not at offset 0");
} }
return new AnyPointer.Builder(rootSegment, location); return new AnyPointer.Builder(rootSegment, location);
} else { } else {

View file

@ -38,7 +38,7 @@ public final class PackedInputStream implements ReadableByteChannel {
if (len == 0) { return 0; } if (len == 0) { return 0; }
if (len % 8 != 0) { if (len % 8 != 0) {
throw new Error("PackedInputStream reads must be word-aligned"); throw new DecodeException("PackedInputStream reads must be word-aligned");
} }
int outPtr = outBuf.position(); int outPtr = outBuf.position();
@ -91,13 +91,13 @@ public final class PackedInputStream implements ReadableByteChannel {
if (tag == 0) { if (tag == 0) {
if (inBuf.remaining() == 0) { if (inBuf.remaining() == 0) {
throw new Error("Should always have non-empty buffer here."); throw new DecodeException("Should always have non-empty buffer here.");
} }
int runLength = (0xff & (int)inBuf.get()) * 8; int runLength = (0xff & (int)inBuf.get()) * 8;
if (runLength > outEnd - outPtr) { if (runLength > outEnd - outPtr) {
throw new Error("Packed input did not end cleanly on a segment boundary"); throw new DecodeException("Packed input did not end cleanly on a segment boundary");
} }
for (int i = 0; i < runLength; ++i) { for (int i = 0; i < runLength; ++i) {

View file

@ -210,7 +210,7 @@ public class StructBuilder {
// (but ignore empty sections). // (but ignore empty sections).
if ((sharedDataSize == 0 || other.data == this.data) && if ((sharedDataSize == 0 || other.data == this.data) &&
(sharedPointerCount == 0 || other.pointers == this.pointers)) { (sharedPointerCount == 0 || other.pointers == this.pointers)) {
throw new Error("Only one of the section pointers is pointing to ourself"); throw new RuntimeException("Only one of the section pointers is pointing to ourself");
} }
// So `other` appears to be a reader for this same struct. No copying is needed. // So `other` appears to be a reader for this same struct. No copying is needed.

View file

@ -22,6 +22,7 @@
package org.capnproto; package org.capnproto;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
public final class Text { public final class Text {
public static final class Factory implements public static final class Factory implements
@ -88,14 +89,10 @@ public final class Text {
} }
public Reader(String value) { public Reader(String value) {
try { byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
byte[] bytes = value.getBytes("UTF-8"); this.buffer = ByteBuffer.wrap(bytes);
this.buffer = ByteBuffer.wrap(bytes); this.offset = 0;
this.offset = 0; this.size = bytes.length;
this.size = bytes.length;
} catch (java.io.UnsupportedEncodingException e) {
throw new Error("UTF-8 is unsupported");
}
} }
public final int size() { public final int size() {
@ -118,11 +115,7 @@ public final class Text {
dup.position(this.offset); dup.position(this.offset);
dup.get(bytes, 0, this.size); dup.get(bytes, 0, this.size);
try { return new String(bytes, StandardCharsets.UTF_8);
return new String(bytes, "UTF-8");
} catch (java.io.UnsupportedEncodingException e) {
throw new Error("UTF-8 is unsupported");
}
} }
} }
@ -160,11 +153,7 @@ public final class Text {
dup.position(this.offset); dup.position(this.offset);
dup.get(bytes, 0, this.size); dup.get(bytes, 0, this.size);
try { return new String(bytes, StandardCharsets.UTF_8);
return new String(bytes, "UTF-8");
} catch (java.io.UnsupportedEncodingException e) {
throw new Error("UTF-8 is unsupported");
}
} }
} }

View file

@ -257,7 +257,7 @@ final class WireHelpers {
case ElementSize.INLINE_COMPOSITE: { case ElementSize.INLINE_COMPOSITE: {
long elementTag = segment.get(ptr); long elementTag = segment.get(ptr);
if (WirePointer.kind(elementTag) != WirePointer.STRUCT) { if (WirePointer.kind(elementTag) != WirePointer.STRUCT) {
throw new Error("Don't know how to handle non-STRUCT inline composite."); throw new RuntimeException("Don't know how to handle non-STRUCT inline composite.");
} }
int dataSize = StructPointer.dataSize(elementTag); int dataSize = StructPointer.dataSize(elementTag);
int pointerCount = StructPointer.ptrCount(elementTag); int pointerCount = StructPointer.ptrCount(elementTag);
@ -280,9 +280,9 @@ final class WireHelpers {
break; break;
} }
case WirePointer.FAR: case WirePointer.FAR:
throw new Error("Unexpected FAR pointer."); throw new IllegalArgumentException("Unexpected FAR pointer.");
case WirePointer.OTHER: case WirePointer.OTHER:
throw new Error("Unexpected OTHER pointer."); throw new IllegalArgumentException("Unexpected OTHER pointer.");
} }
} }
@ -411,7 +411,7 @@ final class WireHelpers {
if (defaultSegment == null) { if (defaultSegment == null) {
return initStructPointer(factory, refOffset, segment, size); return initStructPointer(factory, refOffset, segment, size);
} else { } else {
throw new Error("unimplemented"); throw new RuntimeException("unimplemented");
} }
} }
FollowBuilderFarsResult resolved = followBuilderFars(ref, target, segment); FollowBuilderFarsResult resolved = followBuilderFars(ref, target, segment);
@ -525,7 +525,7 @@ final class WireHelpers {
int origRefTarget = WirePointer.target(origRefOffset, origRef); int origRefTarget = WirePointer.target(origRefOffset, origRef);
if (WirePointer.isNull(origRef)) { if (WirePointer.isNull(origRef)) {
throw new Error("unimplemented"); throw new RuntimeException("unimplemented");
} }
//# We must verify that the pointer has the right size. Unlike //# We must verify that the pointer has the right size. Unlike
@ -551,7 +551,7 @@ final class WireHelpers {
//# therefore never need to upgrade the data in this case, //# therefore never need to upgrade the data in this case,
//# but we do need to validate that it is a valid upgrade //# but we do need to validate that it is a valid upgrade
//# from what we expected. //# from what we expected.
throw new Error("unimplemented"); throw new RuntimeException("unimplemented");
} else { } else {
int dataSize = ElementSize.dataBitsPerElement(oldSize); int dataSize = ElementSize.dataBitsPerElement(oldSize);
int pointerCount = ElementSize.pointersPerElement(oldSize); int pointerCount = ElementSize.pointersPerElement(oldSize);
@ -581,7 +581,7 @@ final class WireHelpers {
int origRefTarget = WirePointer.target(origRefOffset, origRef); int origRefTarget = WirePointer.target(origRefOffset, origRef);
if (WirePointer.isNull(origRef)) { if (WirePointer.isNull(origRef)) {
throw new Error("unimplemented"); throw new RuntimeException("unimplemented");
} }
//# We must verify that the pointer has the right size and potentially upgrade it if not. //# We must verify that the pointer has the right size and potentially upgrade it if not.
@ -684,7 +684,7 @@ final class WireHelpers {
//# Upgrading to an inline composite list. //# Upgrading to an inline composite list.
if (oldSize == ElementSize.BIT) { if (oldSize == ElementSize.BIT) {
throw new Error("Found bit list where struct list was expected; " + throw new RuntimeException("Found bit list where struct list was expected; " +
"upgrading boolean lists to struct is no longer supported."); "upgrading boolean lists to struct is no longer supported.");
} }
@ -935,7 +935,7 @@ final class WireHelpers {
dataSize, value.pointerCount); dataSize, value.pointerCount);
if (value.dataSize == 1) { if (value.dataSize == 1) {
throw new Error("single bit case not handled"); throw new RuntimeException("single bit case not handled");
} else { } else {
memcpy(allocation.segment.buffer, allocation.ptr * Constants.BYTES_PER_WORD, memcpy(allocation.segment.buffer, allocation.ptr * Constants.BYTES_PER_WORD,
value.segment.buffer, value.data, value.dataSize / Constants.BITS_PER_BYTE); value.segment.buffer, value.data, value.dataSize / Constants.BITS_PER_BYTE);
@ -974,7 +974,7 @@ final class WireHelpers {
case 32: elementSize = ElementSize.FOUR_BYTES; break; case 32: elementSize = ElementSize.FOUR_BYTES; break;
case 64: elementSize = ElementSize.EIGHT_BYTES; break; case 64: elementSize = ElementSize.EIGHT_BYTES; break;
default: default:
throw new Error("invalid list step size: " + value.step); throw new RuntimeException("invalid list step size: " + value.step);
} }
ListPointer.set(allocation.segment.buffer, allocation.refOffset, elementSize, value.elementCount); ListPointer.set(allocation.segment.buffer, allocation.refOffset, elementSize, value.elementCount);
@ -1124,9 +1124,9 @@ final class WireHelpers {
case WirePointer.FAR : case WirePointer.FAR :
throw new DecodeException("Unexpected FAR pointer."); throw new DecodeException("Unexpected FAR pointer.");
case WirePointer.OTHER : case WirePointer.OTHER :
throw new Error("copyPointer is unimplemented for OTHER pointers"); throw new RuntimeException("copyPointer is unimplemented for OTHER pointers");
} }
throw new Error("unreachable"); throw new RuntimeException("unreachable");
} }
static <T> T readListPointer(ListReader.Factory<T> factory, static <T> T readListPointer(ListReader.Factory<T> factory,
@ -1150,7 +1150,7 @@ final class WireHelpers {
} }
if (nestingLimit <= 0) { if (nestingLimit <= 0) {
throw new Error("nesting limit exceeded"); throw new DecodeException("nesting limit exceeded");
} }
int refTarget = WirePointer.target(refOffset, ref); int refTarget = WirePointer.target(refOffset, ref);