Kotlin: Internalize and move getException to WebGpuRuntimeException companion
Moves the top-level getException function into the
WebGpuRuntimeException companion object and marks it as internal. This
prevents the public API surface for exception creation by devs.
BUG: b/462818219
Change-Id: I57318528a5d7157a67d7436ff88f57798d1ee9ca
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/279035
Reviewed-by: Jim Blackler <jimblackler@google.com>
Commit-Queue: Mridul Goyal <mridulgoyal@google.com>
diff --git a/generator/templates/art/api_kotlin_async_helpers.kt b/generator/templates/art/api_kotlin_async_helpers.kt
index a20f100..6b006a3 100644
--- a/generator/templates/art/api_kotlin_async_helpers.kt
+++ b/generator/templates/art/api_kotlin_async_helpers.kt
@@ -94,7 +94,7 @@
{% if ns.payload_arg and ns.payload_arg.type.name.get() == 'error type' %}
//* If the payload is a Dawn error type, create the matching exception.
else if ({{ as_varName(ns.payload_arg.name) }} != ErrorType.NoError) {
- it.resumeWithException(getException({{ as_varName(ns.payload_arg.name) }}, {{ as_varName(ns.message_arg.name) }}))
+ it.resumeWithException(WebGpuRuntimeException.create({{ as_varName(ns.payload_arg.name) }}, {{ as_varName(ns.message_arg.name) }}))
}
{% endif %}
{% if ns.payload_arg.optional %}
diff --git a/generator/templates/art/api_kotlin_exceptions.kt b/generator/templates/art/api_kotlin_exceptions.kt
index a769ac1..c2fd14a 100644
--- a/generator/templates/art/api_kotlin_exceptions.kt
+++ b/generator/templates/art/api_kotlin_exceptions.kt
@@ -60,7 +60,23 @@
/**
* Base class for exceptions that can happen at runtime.
*/
-public open class WebGpuRuntimeException(message: String): Exception(message)
+public open class WebGpuRuntimeException(message: String): Exception(message) {
+ public companion object {
+ /**
+ * Create the exception for the appropriate error type.
+ * @param type The [{{ kotlin_name(ns.error) }}].
+ * @param message A human-readable message describing the error.
+ */
+ @JvmStatic
+ internal fun create(@{{ kotlin_name(ns.error) }} type: Int, message: String): WebGpuRuntimeException =
+ when (type) {
+ {% for value in ns.error.values if value.name.get() != "no error" %}
+ {{ kotlin_name(ns.error) }}.{{value.name.CamelCase()}} -> {{value.name.CamelCase()}}Exception(message)
+ {% endfor %}
+ else -> UnknownException(message)
+ }
+ }
+}
{% for value in ns.error.values if value.name.get() != "no error" %}
/**
@@ -72,19 +88,6 @@
{% endfor %}
-/**
- * Create the exception for the appropriate error type.
- * @param type The [{{ kotlin_name(ns.error) }}].
- * @param message A human-readable message describing the device loss.
- */
-public fun getException(@{{ kotlin_name(ns.error) }} type: Int, message: String): WebGpuRuntimeException =
- when (type) {
- {% for value in ns.error.values if value.name.get() != "no error" %}
- {{ kotlin_name(ns.error) }}.{{value.name.CamelCase()}} -> {{value.name.CamelCase()}}Exception(message)
- {% endfor %}
- else -> UnknownException(message)
- }
-
//* Generate a custom exception for every enum ending 'status'.
//* 'status' is renamed 'web gpu status'.
{% for enum in by_category['enum'] if include_enum(enum) and enum.name.chunks[-1] == 'status' %}
diff --git a/tools/android/webgpu/src/main/java/androidx/webgpu/helper/WebGpu.kt b/tools/android/webgpu/src/main/java/androidx/webgpu/helper/WebGpu.kt
index 6913134..005325c 100644
--- a/tools/android/webgpu/src/main/java/androidx/webgpu/helper/WebGpu.kt
+++ b/tools/android/webgpu/src/main/java/androidx/webgpu/helper/WebGpu.kt
@@ -27,7 +27,7 @@
import androidx.webgpu.UnknownException
import androidx.webgpu.ValidationException
import androidx.webgpu.GPU.createInstance
-import androidx.webgpu.getException
+import androidx.webgpu.WebGpuRuntimeException
import androidx.webgpu.helper.Util.windowFromSurface
import java.util.concurrent.Executor
@@ -121,7 +121,7 @@
private val defaultUncapturedErrorCallback
get(): UncapturedErrorCallback {
return UncapturedErrorCallback { _, type, message ->
- throw getException(type, message)
+ throw WebGpuRuntimeException.create(type, message)
}
}