DesktopHTMLViewer Error Event

Using 2025.2 mac

I am trying to catch a 404 status in a HTMLViewer. The docs list the Error event with parameters ErrorNumber, ErrorMessage, which would be what I need. However the actual event as implemented in the code shows “Error as RuntimeException” as the only parameter. Moreover, it is not triggered by a 404 status.

Is there a way to read the http status of the loaded page?

I guess the documentation needs to be updated anyway.

Unfortunately I don’t know of a way to retrieve the status code via the DesktopHTMLViewer. HTTP status isn’t available via Javascript. We should have a feature request for this.

The best you could probably do is to verify each page with a subsequent URLConnection.

Sub DocumentComplete(url as String) Handles DocumentComplete
  var c as new UrlConnection 
  Try
    Call c.SendSync("GET", url, 10)
  Catch e as RuntimeException
     
  End Try
  if c.HTTPStatusCode = 404 then
    MessageBox "404 Error"
  end if
End Sub

I realize this is imperfect with dynamic pages, logins and such, but it’s all I could come up with.

2 Likes

Thank you, that’s an interesting idea and I think it could work in my configuration - an integrated help viewer that loads static web pages.

Maybe a feature request for an HTTPException to the HTMLViewer error event would be useful.
This would allow for a good an easy way to differentiate between exceptions and handle such accordingly.

Well WKWebView doesn’t expose the http status in a public API so there’s not much Xojo can do on macOS in any case.

1 Like

I was under the impression that there was a NSHTTPURLResponse which you could pull a status code from. It’s been a long time since I dug into it though.

WKNavigationResponse has this info.

I just checked apples docs again. I don’t see how WKNavigationResponse is related to NSURLHTTPResponse which is what you’d need.

Like this?

Just a quick test with WKWebview from the MBS plugin.

1 Like

Sure, that’s the request, not the response.

1 Like

Scroll down to:

How to Handle HTTP Responses

The code below it can be translated to xojo.

Or use MBS:

Bottom of the page NSURLresponseMBS

1 Like

Thanks @DerkJ, you did a lot more research than I had time for.

1 Like

Thanks DerkJ, that is helpful!

1 Like