Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up JS wrapper exceptions #51

Merged
merged 1 commit into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -34,33 +35,31 @@ 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()

override fun toString(): String = "Buffer@${hashCode()}"

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<buffer_Buffer>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class WrapperUnitTest {
@Test
fun givenBuffer_whenFromDynamicAndNotActuallyABuffer_thenThrowsException() {
val stats = FILE_LOREM_IPSUM.stat().unwrap()
assertFailsWith<IOException> { Buffer.wrap(stats) }
assertFailsWith<IllegalArgumentException> { Buffer.wrap(stats) }
}

@Test
Expand Down
Loading