The documentation of syscall/js says that js.Value.Get panics when a value is not a JavaScript object, so you should be able to “catch the exception” just like you would recover from a panic in other Go code:
func tryGet(v js.Value, p string) (result js.Value, err error) {
defer func() {
if x := recover(); x != nil {
var ok bool
if err, ok = x.(error); !ok {
err = fmt.Errorf("%v", x)
}
}
}()
return v.Get(p), nil
}
Thank you @skillian
Unfortunately it does not work. The JS call exception does not make the go code panicking, so not recovery possible.
The go wasm code only… stops… at the .Get("localtorage") line
On my computer, accessing window.localStorage did not throw an exception, but I got an error as the 2nd return value from tryGet if I used an invalid attribute name.