scala-js/scala-js-website

document how interop works with exceptions

aappddeevv opened this issue · 5 comments

i.e. throwing an exception in scala, what can js catch? and vice versa.

I did not see this in the docs.

sjrd commented

The gist is:

  • Scala.js throws an instance of js.JavaScriptException -> JS catches the wrapped value
  • Scala.js throws something else (necessarily a Throwable) -> JS catches it as is
  • JS throws an instance of Scala.js' Throwable -> Scala.js catches it as is
  • JS throws something else -> Scala.js catches it as wrapped in a js.JavaScriptException

What if you need to throw a Promise? i.e. something that is a not a Throwable and have JS catch it?

I cast a Promise to a Throwable to make scala.js throw and I'm almost to the point where I can test to see if js is catching it. Should I expect that to work?

I know this is js using "exceptions as flow control" but it is what it is for the moment.

sjrd commented

First bullet above -> throw js.JavaScriptException(thePromise) means that JS will catch thePromise.

You cannot cast thePromise to Throwable because it's not a Throwable.

I think that covers the interop cases. Thanks.

I'll put a PR together for this content in the docs.