js.ValueOf for an error

83 views
Skip to first unread message

Will Hawkins

unread,
Dec 23, 2022, 9:33:21 AM12/23/22
to golang-nuts
Hello everyone!

First, thank you *all* (I mean this great community) for the Go language and especially its support for a wasm target.

As I was attempting to finish up a CL (https://go-review.googlesource.com/c/go/+/458395) I found occasion to need to great a JS Error based on a Go error. I was hoping to use the JS Error constructor that includes an option object to specify a cause and value (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) by using the actual go error. Here's what I was thinking:

readErrCause := js.Global().Get("Object").New()
readErrCause.Set("code", readErr.Error())
readErrCause.Set("value", js.ValueOf(readErr))
readErr := js.Global().Get("Error").New("ReadFull failed", readErrCause)

(where readErr is of the `error` type, of course)

Unfortunately,
readErrCause.Set("value", js.ValueOf(readErr))

does not work because, as the comment so perfectly says, ValueOf

// Panics if x is not one of the expected types.

I am absolutely up for proposing and implementing a small addition to the "js package" if people think it would be helpful but I wanted to check here first.

Again, thank you for all the great work that you *all* do!!

Sincerely,
Will



Brian Candler

unread,
Dec 23, 2022, 10:23:27 AM12/23/22
to golang-nuts
I don't know how a raw Go interface value would be usable in Javascript.  As an alternative, would it be any good to use sprintf("%T", readErr) as the code, and readErr.Error() as the value? At least those are both strings.

Will Hawkins

unread,
Dec 23, 2022, 11:05:20 AM12/23/22
to Brian Candler, golang-nuts
I agree. That is what I settled on, basically. Thanks for the suggestion!

Will
> --
> You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/zwwA1wRjA58/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/08d9eb2b-49bb-4a8a-8b81-d54b70a24fc0n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages