B4J Tutorial [Server] Custom error pages

Discussion in 'B4J Tutorials' started by Erel, Mar 13, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

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

    [​IMG]

    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:
    Code:
    Dim err As Map
    err.Initialize
    err.Put(
    404"/404.html"'page not found
    err.Put(500"/500.html"'server error
    srvr.SetCustomErrorPages(err)

    [​IMG]

    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):
    Code:
    err.Put("org.eclipse.jetty.server.error_page.global""/page_that_will_handle_all_errors.html")
     
    AscySoft and wl like this.
  2. wl

    wl Well-Known Member Licensed User

    Any chance of some other extra's, such as support for cookies or HTML templates (freemarker) ?

    Thanks
     
    Last edited: Mar 13, 2014
  3. Erel

    Erel Administrator Staff Member Licensed User

    This is not the place to discuss other features...

    Cookies and user sessions are supported in v1.80: B4J v1.80 BETA is released
     
  4. tchart

    tchart Active Member Licensed User

    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?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Instead of redirecting to /404.html you can redirect to /somehandler.

    In the main module add this handler:
    Code:
    srvr.AddHandler("/somehandler""SomeHandler"False)
     
  6. tchart

    tchart Active Member Licensed User

    Ah now I get it! Thanks Erel
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice