diff --git a/library/file/src/jsMain/kotlin/io/matthewnelson/kmp/file/FileJs.kt b/library/file/src/jsMain/kotlin/io/matthewnelson/kmp/file/FileJs.kt index e8ba9a6..77db7fa 100644 --- a/library/file/src/jsMain/kotlin/io/matthewnelson/kmp/file/FileJs.kt +++ b/library/file/src/jsMain/kotlin/io/matthewnelson/kmp/file/FileJs.kt @@ -51,14 +51,8 @@ public fun File.write(data: Buffer) { public fun Throwable.toIOException(): IOException { if (this is IOException) return this - val code = try { - errorCode - } catch (_: Throwable) { - null - } - - return when (code) { + return when (errorCode) { "ENOENT" -> FileNotFoundException(message) - else -> IOException(message) + else -> IOException(this) } } diff --git a/library/file/src/jsMain/kotlin/io/matthewnelson/kmp/file/Wrappers.kt b/library/file/src/jsMain/kotlin/io/matthewnelson/kmp/file/Wrappers.kt index 57ce706..c1cfb1f 100644 --- a/library/file/src/jsMain/kotlin/io/matthewnelson/kmp/file/Wrappers.kt +++ b/library/file/src/jsMain/kotlin/io/matthewnelson/kmp/file/Wrappers.kt @@ -16,6 +16,7 @@ package io.matthewnelson.kmp.file import io.matthewnelson.kmp.file.internal.buffer_Buffer +import io.matthewnelson.kmp.file.internal.errorCode import io.matthewnelson.kmp.file.internal.fs_Stats /** @@ -34,22 +35,20 @@ public value class Buffer internal constructor( public val length: Long get() = value.length.toLong() public fun fill() { value.fill() } - // @Throws(IOException::class) + // @Throws(IndexOutOfBoundsException::class, IllegalArgumentException::class) public fun readInt8(index: Number): Byte = try { value.readInt8(index) as Byte } catch (t: Throwable) { - throw t.toIOException() + throw when (t.errorCode) { + "ERR_OUT_OF_RANGE" -> IndexOutOfBoundsException(t.message) + else -> IllegalArgumentException(t) + } } - // @Throws(IOException::class) public fun toUtf8( start: Number = 0, end: Number = this.length, - ): String = try { - value.toString("utf8", start, end) - } catch (t: Throwable) { - throw t.toIOException() - } + ): String = value.toString("utf8", start, end) public fun unwrap(): dynamic = value.asDynamic() @@ -57,10 +56,10 @@ public value class Buffer internal constructor( public companion object { - // @Throws(IOException::class) + // @Throws(IllegalArgumentException::class) public fun wrap(buffer: dynamic): Buffer { if (!buffer_Buffer.isBuffer(buffer)) { - throw IOException("Not a buffer") + throw IllegalArgumentException("Not a buffer") } return Buffer(buffer.unsafeCast()) diff --git a/library/file/src/jsMain/kotlin/io/matthewnelson/kmp/file/internal/JsPlatform.kt b/library/file/src/jsMain/kotlin/io/matthewnelson/kmp/file/internal/JsPlatform.kt index f2415b2..c8ca7f8 100644 --- a/library/file/src/jsMain/kotlin/io/matthewnelson/kmp/file/internal/JsPlatform.kt +++ b/library/file/src/jsMain/kotlin/io/matthewnelson/kmp/file/internal/JsPlatform.kt @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ -@file:Suppress("FunctionName", "ACTUAL_ANNOTATIONS_NOT_MATCH_EXPECT") +@file:Suppress("FunctionName", "ACTUAL_ANNOTATIONS_NOT_MATCH_EXPECT", "KotlinRedundantDiagnosticSuppress") package io.matthewnelson.kmp.file.internal @@ -43,6 +43,7 @@ internal actual val IsWindows: Boolean by lazy { } } +// @Throws(IOException::class) @Suppress("NOTHING_TO_INLINE") internal actual inline fun File.platformReadBytes(): ByteArray = try { val buffer = read() @@ -60,6 +61,7 @@ internal actual inline fun File.platformReadBytes(): ByteArray = try { throw t.toIOException() } +// @Throws(IOException::class) @Suppress("NOTHING_TO_INLINE") internal actual inline fun File.platformReadUtf8(): String = try { val buffer = read() @@ -77,6 +79,7 @@ internal actual inline fun File.platformReadUtf8(): String = try { throw t.toIOException() } +// @Throws(IOException::class) @Suppress("NOTHING_TO_INLINE") internal actual inline fun File.platformWriteBytes(array: ByteArray) { try { @@ -86,6 +89,7 @@ internal actual inline fun File.platformWriteBytes(array: ByteArray) { } } +// @Throws(IOException::class) @Suppress("NOTHING_TO_INLINE") internal actual inline fun File.platformWriteUtf8(text: String) { try { @@ -117,6 +121,7 @@ internal actual inline fun Path.isAbsolute(): Boolean { return path_isAbsolute(this) } +// @Throws(IOException::class) internal actual fun fs_chmod(path: String, mode: String) { try { fs_chmodSync(path, mode) @@ -125,6 +130,7 @@ internal actual fun fs_chmod(path: String, mode: String) { } } +// @Throws(IOException::class) internal actual fun fs_remove(path: String): Boolean { try { fs_unlinkSync(path) @@ -163,6 +169,7 @@ internal actual fun fs_mkdir(path: String): Boolean { } } +// @Throws(IOException::class) internal actual fun fs_realpath(path: String): String { return try { fs_realpathSync(path) @@ -171,4 +178,9 @@ internal actual fun fs_realpath(path: String): String { } } -internal val Throwable.errorCode: dynamic get() = asDynamic().code +@Suppress("NOTHING_TO_INLINE", "REDUNDANT_NULLABLE") +internal inline val Throwable.errorCode: String? get() = try { + asDynamic().code as String +} catch (_: Throwable) { + null +} diff --git a/library/file/src/jsTest/kotlin/io/matthewnelson/kmp/file/WrapperUnitTest.kt b/library/file/src/jsTest/kotlin/io/matthewnelson/kmp/file/WrapperUnitTest.kt index f46d1ff..5f209c0 100644 --- a/library/file/src/jsTest/kotlin/io/matthewnelson/kmp/file/WrapperUnitTest.kt +++ b/library/file/src/jsTest/kotlin/io/matthewnelson/kmp/file/WrapperUnitTest.kt @@ -39,7 +39,7 @@ class WrapperUnitTest { @Test fun givenBuffer_whenFromDynamicAndNotActuallyABuffer_thenThrowsException() { val stats = FILE_LOREM_IPSUM.stat().unwrap() - assertFailsWith { Buffer.wrap(stats) } + assertFailsWith { Buffer.wrap(stats) } } @Test