B4J Tutorial [Server] Custom error pages

Starting from B4J v1.80 you can configure custom error pages. This means that instead of the default error page:

SS-2014-03-13_16.43.30.png


You can show any page you like. You have two options, you can use a static error page or a handler to handle the error. The advantage of a handler is that you can show a custom message based on the error.

We need to configure the custom pages before the server is started. The configuration is done with a Map that ties the error pages to the http error codes: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
For example, with this code we set two static error pages that will be loaded from the static resources folder:
B4X:
Dim err As Map
err.Initialize
err.Put(404, "/404.html") 'page not found
err.Put(500, "/500.html") 'server error
srvr.SetCustomErrorPages(err)


SS-2014-03-13_16.52.43.png


The URL can point to a handler instead of a static file.
In that case the status code and error message can be retrieved from the response object that is passed to the Handle method (ServletResponse.StatusCode and ServletResponse.ErrorReason).

You can also define a global error page that will catch all the errors (that were not handled):
B4X:
err.Put("org.eclipse.jetty.server.error_page.global", "/page_that_will_handle_all_errors.html")
 

wl

Well-Known Member
Licensed User
Longtime User
Any chance of some other extra's, such as support for cookies or HTML templates (freemarker) ?

Thanks
 
Last edited:

tchart

Well-Known Member
Licensed User
Longtime User
Erel said:
The URL can point to a handler instead of a static file.

Erel, can you provide a code sample for handling errors using a handler? I cant seem to figure it out. Which URL are you referring to?
 

tchart

Well-Known Member
Licensed User
Longtime User
Just a note for anyone who comes across this tutorial, custom pages for 500 errors do not work in debug mode.
 
Top