diff --git a/js/js.libraries/src/core/builtins.kt b/js/js.libraries/src/core/builtins.kt index 9ca9c4a8d9f..ba103dfac0f 100644 --- a/js/js.libraries/src/core/builtins.kt +++ b/js/js.libraries/src/core/builtins.kt @@ -61,7 +61,13 @@ internal fun captureStack(baseClass: JsClass, instance: Throwable) @JsName("newThrowable") internal fun newThrowable(message: String?, cause: Throwable?): Throwable { val throwable = js("new Error()") - throwable.message = if (jsTypeOf(message) == "undefined" && cause != null) cause.toString() else message + throwable.message = if (jsTypeOf(message) == "undefined") { + if (cause != null) cause.toString() else null + } + else { + message + } throwable.cause = cause + throwable.name = "Throwable" return throwable } \ No newline at end of file diff --git a/js/js.translator/testData/box/expression/try/exceptionToString.kt b/js/js.translator/testData/box/expression/try/exceptionToString.kt index 6bc6efb5c1f..176457674e0 100644 --- a/js/js.translator/testData/box/expression/try/exceptionToString.kt +++ b/js/js.translator/testData/box/expression/try/exceptionToString.kt @@ -13,6 +13,8 @@ fun check(e: Throwable, expectedString: String) { } fun box(): String { + check(Throwable(), "Throwable: null") + check(Throwable("ccc"), "Throwable: ccc") check(Exception(), "Exception: null") check(Exception("bbb"), "Exception: bbb") check(MyException(), "MyException: null")