Please help me understand error messages process

I will appreciate any help or pointers regarding this question.
When I get error message, say, “server not found” for a URL like this:

The URL address does not change. It still shows the URL of the requested site, but the content is an error message.

Where is this message retrieved from? if I want to customize it if it occurred using page-mod for example, I need to know the URL for this error. But, in the error pages case, I do not know their URL.

Are there any methods other than page-mod to customize an error page before it appears on the tab? in a way that distinguish error types? Any suggestions?

Above page is result of DNS failure.
You can listen to headers for server responses before paeg is shown.

Do you know which field exactly in the header that I should examine?

“Server not found” is due to DNS error. That happens before any HEADER is received.

Browser checks URL for DNS record
If found, then sends a request to that server.
Server replies and those are the HTTP response status codes
For all errors page (except the DNS error) you can check the HEADER status & statusText

Firefox DNS was not scriptable and WebExtension is similar.

Try this API and see if it gives you what you need. I have not tested the API myself, so I don’t know.

The API seems almost close to what I am asking about but there are two problems: 1) it does not specify the error and treats all errors the same. 2) my extension is already built in SDK. It is for local use only so I do not have issues with the deprecation until Nov. when the browser new version comes out.

But there are some errors such as “Secure Connection Failed” when the certificate is expired for example or self-signed, where there no response header returned. How to observe such an event?