Skip to content

Commit d8df632

Browse files
committed
Throw the correct exception type
1 parent 51f39b5 commit d8df632

File tree

6 files changed

+112
-58
lines changed

6 files changed

+112
-58
lines changed

runtime/runtime-core/api/runtime-core.api

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,12 @@ public final class aws/smithy/kotlin/runtime/io/ClosedWriteChannelException : ja
760760
public synthetic fun <init> (Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
761761
}
762762

763+
public class aws/smithy/kotlin/runtime/io/EOFException : java/io/EOFException {
764+
public fun <init> ()V
765+
public fun <init> (Ljava/lang/String;)V
766+
public fun <init> (Ljava/lang/String;Ljava/lang/Throwable;)V
767+
}
768+
763769
public final class aws/smithy/kotlin/runtime/io/GzipByteReadChannel : aws/smithy/kotlin/runtime/io/SdkByteReadChannel {
764770
public fun <init> (Laws/smithy/kotlin/runtime/io/SdkByteReadChannel;)V
765771
public fun cancel (Ljava/lang/Throwable;)Z

runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/io/BuffereredSourceAdapter.kt

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ internal expect class BufferedSourceAdapter(source: okio.BufferedSource) : SdkBu
3232
override fun close()
3333
}
3434

35+
/**
36+
* Used to wrap calls to Okio, catching Okio exceptions (e.g. okio.EOFException) and throwing our own (e.g. aws.smithy.kotlin.runtime.io.EOFException).
37+
*/
38+
internal inline fun <T> SdkBufferedSource.wrapOkio(block: SdkBufferedSource.() -> T): T = try {
39+
block()
40+
} catch (e: okio.EOFException) {
41+
throw EOFException("Okio operation failed", e)
42+
} catch (e: okio.IOException) {
43+
throw IOException("Okio operation failed", e)
44+
}
45+
3546
// base class that fills in most of the common implementation, platforms just need to implement the platform specific
3647
// part of the interface
3748
internal abstract class AbstractBufferedSourceAdapter(
@@ -40,45 +51,57 @@ internal abstract class AbstractBufferedSourceAdapter(
4051
override val buffer: SdkBuffer
4152
get() = delegate.buffer.toSdk()
4253

43-
override fun skip(byteCount: Long): Unit = delegate.skip(byteCount)
54+
override fun skip(byteCount: Long): Unit = wrapOkio { delegate.skip(byteCount) }
4455

45-
override fun readByte(): Byte = delegate.readByte()
56+
override fun readByte(): Byte = wrapOkio { delegate.readByte() }
4657

47-
override fun readShort(): Short = delegate.readShort()
58+
override fun readShort(): Short = wrapOkio { delegate.readShort() }
4859

49-
override fun readShortLe(): Short = delegate.readShortLe()
60+
override fun readShortLe(): Short = wrapOkio { delegate.readShortLe() }
5061

51-
override fun readLong(): Long = delegate.readLong()
62+
override fun readLong(): Long = wrapOkio { delegate.readLong() }
5263

53-
override fun readLongLe(): Long = delegate.readLongLe()
64+
override fun readLongLe(): Long = wrapOkio { delegate.readLongLe() }
5465

55-
override fun readInt(): Int = delegate.readInt()
66+
override fun readInt(): Int = wrapOkio { delegate.readInt() }
5667

57-
override fun readIntLe(): Int = delegate.readIntLe()
68+
override fun readIntLe(): Int = wrapOkio { delegate.readIntLe() }
5869

59-
override fun readAll(sink: SdkSink): Long =
70+
override fun readAll(sink: SdkSink): Long = wrapOkio {
6071
delegate.readAll(sink.toOkio())
72+
}
6173

62-
override fun read(sink: ByteArray, offset: Int, limit: Int): Int =
74+
override fun read(sink: ByteArray, offset: Int, limit: Int): Int = wrapOkio {
6375
delegate.read(sink, offset, limit)
76+
}
6477

65-
override fun read(sink: SdkBuffer, limit: Long): Long =
78+
override fun read(sink: SdkBuffer, limit: Long): Long = wrapOkio {
6679
delegate.read(sink.toOkio(), limit)
80+
}
6781

68-
override fun readByteArray(): ByteArray = delegate.readByteArray()
82+
override fun readByteArray(): ByteArray = wrapOkio { delegate.readByteArray() }
6983

70-
override fun readByteArray(byteCount: Long): ByteArray = delegate.readByteArray(byteCount)
84+
override fun readByteArray(byteCount: Long): ByteArray = wrapOkio {
85+
delegate.readByteArray(byteCount)
86+
}
7187

72-
override fun readUtf8(): String = delegate.readUtf8()
88+
override fun readUtf8(): String = wrapOkio { delegate.readUtf8() }
7389

74-
override fun readUtf8(byteCount: Long): String = delegate.readUtf8(byteCount)
90+
override fun readUtf8(byteCount: Long): String = wrapOkio {
91+
delegate.readUtf8(byteCount)
92+
}
7593

76-
override fun peek(): SdkBufferedSource =
94+
override fun peek(): SdkBufferedSource = wrapOkio {
7795
delegate.peek().toSdk().buffer()
78-
override fun exhausted(): Boolean = delegate.exhausted()
79-
override fun request(byteCount: Long): Boolean = delegate.request(byteCount)
96+
}
97+
98+
override fun exhausted(): Boolean = wrapOkio { delegate.exhausted() }
99+
100+
override fun request(byteCount: Long): Boolean = wrapOkio {
101+
delegate.request(byteCount)
102+
}
80103

81-
override fun require(byteCount: Long): Unit = delegate.require(byteCount)
104+
override fun require(byteCount: Long): Unit = wrapOkio { delegate.require(byteCount) }
82105

83-
override fun close() = delegate.close()
106+
override fun close() = wrapOkio { delegate.close() }
84107
}

runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/io/Exceptions.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ public expect open class IOException(message: String?, cause: Throwable?) : Exce
1010
public constructor(message: String?)
1111
}
1212

13-
public expect open class EOFException(message: String?) : IOException {
13+
public expect open class EOFException(message: String?, cause: Throwable?) : IOException {
1414
public constructor()
15+
public constructor(message: String?)
1516
}
1617

1718
/**

runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/io/internal/BufferOperations.kt

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,95 +9,108 @@ package aws.smithy.kotlin.runtime.io.internal
99

1010
import aws.smithy.kotlin.runtime.io.*
1111

12-
internal inline fun SdkBuffer.commonSkip(byteCount: Long) = inner.skip(byteCount)
12+
/**
13+
* Used to wrap calls to Okio, catching Okio exceptions (e.g. okio.EOFException) and throwing our own (e.g. aws.smithy.kotlin.runtime.io.EOFException).
14+
*/
15+
internal inline fun <T> SdkBuffer.wrapOkio(block: SdkBuffer.() -> T): T = try {
16+
block()
17+
} catch (e: okio.EOFException) {
18+
throw EOFException("Okio operation failed", e)
19+
} catch (e: okio.IOException) {
20+
throw IOException("Okio operation failed", e)
21+
}
22+
23+
internal inline fun SdkBuffer.commonSkip(byteCount: Long) = wrapOkio { inner.skip(byteCount) }
1324

14-
internal inline fun SdkBuffer.commonReadByte(): Byte = inner.readByte()
25+
internal inline fun SdkBuffer.commonReadByte(): Byte = wrapOkio { inner.readByte() }
1526

16-
internal inline fun SdkBuffer.commonReadShort(): Short = inner.readShort()
27+
internal inline fun SdkBuffer.commonReadShort(): Short = wrapOkio { inner.readShort() }
1728

18-
internal inline fun SdkBuffer.commonReadShortLe(): Short = inner.readShortLe()
29+
internal inline fun SdkBuffer.commonReadShortLe(): Short = wrapOkio { inner.readShortLe() }
1930

20-
internal inline fun SdkBuffer.commonReadLong(): Long = inner.readLong()
31+
internal inline fun SdkBuffer.commonReadLong(): Long = wrapOkio { inner.readLong() }
2132

22-
internal inline fun SdkBuffer.commonReadLongLe(): Long = inner.readLongLe()
33+
internal inline fun SdkBuffer.commonReadLongLe(): Long = wrapOkio { inner.readLongLe() }
2334

24-
internal inline fun SdkBuffer.commonReadInt(): Int = inner.readInt()
35+
internal inline fun SdkBuffer.commonReadInt(): Int = wrapOkio { inner.readInt() }
2536

26-
internal inline fun SdkBuffer.commonReadIntLe(): Int = inner.readIntLe()
37+
internal inline fun SdkBuffer.commonReadIntLe(): Int = wrapOkio { inner.readIntLe() }
2738

28-
internal inline fun SdkBuffer.commonReadAll(sink: SdkSink): Long =
29-
inner.readAll(sink.toOkio())
39+
internal inline fun SdkBuffer.commonReadAll(sink: SdkSink): Long = wrapOkio { inner.readAll(sink.toOkio()) }
3040

3141
internal inline fun SdkBuffer.commonRead(sink: ByteArray, offset: Int, limit: Int): Int =
32-
inner.read(sink, offset, limit)
42+
wrapOkio { inner.read(sink, offset, limit) }
3343

3444
internal inline fun SdkBuffer.commonRead(sink: SdkBuffer, limit: Long): Long =
35-
inner.read(sink.inner, limit)
45+
wrapOkio { inner.read(sink.inner, limit) }
3646

37-
internal inline fun SdkBuffer.commonReadByteArray(): ByteArray = inner.readByteArray()
47+
internal inline fun SdkBuffer.commonReadByteArray(): ByteArray = wrapOkio { inner.readByteArray() }
3848

39-
internal inline fun SdkBuffer.commonReadByteArray(byteCount: Long): ByteArray = inner.readByteArray(byteCount)
49+
internal inline fun SdkBuffer.commonReadByteArray(byteCount: Long): ByteArray = wrapOkio { inner.readByteArray(byteCount) }
4050

41-
internal inline fun SdkBuffer.commonReadUtf8(): String = inner.readUtf8()
51+
internal inline fun SdkBuffer.commonReadUtf8(): String = wrapOkio { inner.readUtf8() }
4252

43-
internal inline fun SdkBuffer.commonReadUtf8(byteCount: Long): String = inner.readUtf8(byteCount)
53+
internal inline fun SdkBuffer.commonReadUtf8(byteCount: Long): String = wrapOkio { inner.readUtf8(byteCount) }
4454

45-
internal inline fun SdkBuffer.commonPeek(): SdkBufferedSource = inner.peek().toSdk().buffer()
46-
internal inline fun SdkBuffer.commonExhausted(): Boolean = inner.exhausted()
47-
internal inline fun SdkBuffer.commonRequest(byteCount: Long): Boolean = inner.request(byteCount)
55+
internal inline fun SdkBuffer.commonPeek(): SdkBufferedSource = wrapOkio { inner.peek().toSdk().buffer() }
4856

49-
internal inline fun SdkBuffer.commonRequire(byteCount: Long): Unit = inner.require(byteCount)
57+
internal inline fun SdkBuffer.commonExhausted(): Boolean = wrapOkio { inner.exhausted() }
58+
59+
internal inline fun SdkBuffer.commonRequest(byteCount: Long): Boolean = wrapOkio { inner.request(byteCount) }
60+
61+
internal inline fun SdkBuffer.commonRequire(byteCount: Long) = wrapOkio { inner.require(byteCount) }
5062

5163
internal inline fun SdkBuffer.commonWrite(source: ByteArray, offset: Int, limit: Int) {
52-
inner.write(source, offset, limit)
64+
wrapOkio { inner.write(source, offset, limit) }
5365
}
5466

5567
internal inline fun SdkBuffer.commonWrite(source: SdkSource, byteCount: Long) {
56-
inner.write(source.toOkio(), byteCount)
68+
wrapOkio { inner.write(source.toOkio(), byteCount) }
5769
}
70+
5871
internal inline fun SdkBuffer.commonWrite(source: SdkBuffer, byteCount: Long) {
59-
inner.write(source.toOkio(), byteCount)
72+
wrapOkio { inner.write(source.toOkio(), byteCount) }
6073
}
6174

6275
internal inline fun SdkBuffer.commonWriteAll(source: SdkSource): Long =
63-
inner.writeAll(source.toOkio())
76+
wrapOkio { inner.writeAll(source.toOkio()) }
6477

6578
internal inline fun SdkBuffer.commonWriteUtf8(string: String, start: Int, endExclusive: Int) {
66-
inner.writeUtf8(string, start, endExclusive)
79+
wrapOkio { inner.writeUtf8(string, start, endExclusive) }
6780
}
6881

6982
internal inline fun SdkBuffer.commonWriteByte(x: Byte) {
70-
inner.writeByte(x.toInt())
83+
wrapOkio { inner.writeByte(x.toInt()) }
7184
}
7285

7386
internal inline fun SdkBuffer.commonWriteShort(x: Short) {
74-
inner.writeShort(x.toInt())
87+
wrapOkio { inner.writeShort(x.toInt()) }
7588
}
7689

7790
internal inline fun SdkBuffer.commonWriteShortLe(x: Short) {
78-
inner.writeShortLe(x.toInt())
91+
wrapOkio { inner.writeShortLe(x.toInt()) }
7992
}
8093

8194
internal inline fun SdkBuffer.commonWriteInt(x: Int) {
82-
inner.writeInt(x)
95+
wrapOkio { inner.writeInt(x) }
8396
}
8497

8598
internal inline fun SdkBuffer.commonWriteIntLe(x: Int) {
86-
inner.writeIntLe(x)
99+
wrapOkio { inner.writeIntLe(x) }
87100
}
88101

89102
internal inline fun SdkBuffer.commonWriteLong(x: Long) {
90-
inner.writeLong(x)
103+
wrapOkio { inner.writeLong(x) }
91104
}
92105

93106
internal inline fun SdkBuffer.commonWriteLongLe(x: Long) {
94-
inner.writeLongLe(x)
107+
wrapOkio { inner.writeLongLe(x) }
95108
}
96109

97110
internal inline fun SdkBuffer.commonFlush() {
98-
inner.flush()
111+
wrapOkio { inner.flush() }
99112
}
100113

101114
internal inline fun SdkBuffer.commonClose() {
102-
inner.close()
115+
wrapOkio { inner.close() }
103116
}

runtime/runtime-core/jvm/src/aws/smithy/kotlin/runtime/io/ExceptionsJVM.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,14 @@ package aws.smithy.kotlin.runtime.io
77

88
public actual typealias IOException = java.io.IOException
99

10-
public actual typealias EOFException = java.io.EOFException
10+
public actual open class EOFException actual constructor(
11+
message: String?,
12+
cause: Throwable?
13+
) : java.io.EOFException(message) {
14+
init {
15+
initCause(cause)
16+
}
17+
18+
public actual constructor() : this(null, null)
19+
public actual constructor(message: String?) : this(message, null)
20+
}

runtime/runtime-core/native/src/aws/smithy/kotlin/runtime/io/ExceptionsNative.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public actual open class IOException actual constructor(
1212
public actual constructor(message: String?) : this(message, null)
1313
}
1414

15-
public actual open class EOFException actual constructor(message: String?) : IOException(message) {
16-
public actual constructor() : this(null)
15+
public actual open class EOFException actual constructor(message: String?, cause: Throwable?) : IOException(message) {
16+
public actual constructor() : this(null, null)
17+
public actual constructor(message: String?) : this(message, null)
1718
}

0 commit comments

Comments
 (0)