When targeting the JS backend I want to catch the JavaScript DOMException "A network error occurred.".
So I tried:
var httpRequest = newXMLHttpRequest()
try:
httpRequest.open("GET", cstring"http://ireallydontexist.org/", async=false)
httpRequest.send()
...
except Exception as ex:
echo &"Exception!!! {repr(ex)}"
but this doesn't catch the DOM exception. I suspect Nim's Exception is not 'broad' enough to catch the DOM exception.
Is there another exception in Nim which would catch this?
If you want a complete example using your code:
import strformat
import dom except DOMException
import jsbind
type
# The stdlib's importc is broken for this type.
DOMException {.importc: "DOMException".} = object
type XMLHTTPRequest* = ref object of JSObj # Define the type. JSObj should be the root class for such types.
proc newXMLHTTPRequest*(): XMLHTTPRequest {.jsimportgWithName: "function(){return (window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP')}".}
proc open*(r: XMLHTTPRequest, httpMethod, url: cstring, async = true) {.jsimport.}
proc send*(r: XMLHTTPRequest) {.jsimport.}
proc message*(ex: DOMException): cstring {.importcpp: "#.message", nodecl.}
proc name*(ex: DOMException): cstring {.importcpp: "#.name", nodecl.}
proc `$`*(ex: DOMException): string =
fmt"{ex.name}: {ex.message}"
proc main() =
var httpRequest = newXMLHttpRequest()
try:
httpRequest.open("GET", cstring"http://ireallydontexist.org/", async=false)
httpRequest.send()
except DOMException as ex:
echo fmt"Exception!!! {ex}"
main()
# Outputs:
# Exception!!! NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://ireallydontexist.org/'.
The stdlib's importc is broken for this type.
Report the bug ?.
Thanks for all the detail!
If I understand correctly, it means, that Nim exceptions do not "include" JS exceptions, while Nim's try..except' can catch all exceptions, Nim exceptions and and JS exceptions, right?
Report the bug ?
Well, it depends whether Nim wants to represent JS exceptions through its one exceptions. In my beginner's nativity I think it should, but I am sure there are arguments against it.