Tips_tutorials   >   Studiojs201   >   Web Error Handling

Web Error Handling

We put a Breakpoint near the end of the $construct method if FlagOK was false. Breakpoints, OK, Yes/No, No/Yes messages, and any modal prompts are a big no, no, with web related code. The reason is that the prompt will show up on the server and no response will be sent to the web browser. The Omnis Web App Server will be stuck and unable to responds to any further requests from other web browsers. You get the picture. Someone will have to go to the server, click the OK button on the dialog window before any new requests can be processed. Not a good thing!

We therefore need to add as method to the error handler that we created in Studio 103 and add a new method to oHTMLTools.

Error Handler Method

The error handler has a $promptonceLastError method which opens a modal prompt. We need to add a method which passes back the last error message text, rather than opening a prompt.

  1. F2 Browser > ContactsWeb > double-click oErrorHandler
  2. Add a new method, $getonceLastError
  3. Add the following parameters to the method
    1. pfRetMssg - Field reference - Pass back the last error message.
    2. pfRetMethod - Field reference - Pass back the method which logged the last error.

  4. Add the following code to the method. (You can copy the code from $promptonceLastError and then modify it.)

; Only return the error message values the first time this method is called.
If iErrorsList.prompted=kFalse
   
   ; Set the prompted flag to true to avoid multiple prompts of the same error message.
   Calculate iErrorsList.prompted as kTrue
   
   ; Pass the error message text and method name to the sender.
   Calculate pfRetMssg as iErrorsList.errormssg
   Calculate pfRetMethod as con(iErrorsList.libname,'/',iErrorsList.classname,'/',iErrorsList.methodname)
   
End If
Quit method kTrue

HTML Tools Method

We will add a $retHTMLLastError method to oHTMLTools. This method will get the last error from the error handler, and then format it into an HTML page that is ready to return to the web browser via the Omnis Web App Server.

  1. Add a method, $retLastErrorHTML, to oHTMLTools.
  2. Add the following code to the method.

; Get the last error message logged with the error handler.
Do errhndlr.$getonceLastError(Mssg,Method) Returns FlagOK
If len(Mssg)=0
   Calculate Mssg as "An error occured, but was not logged to the error handler. Please notify the webmaster."
End If

Calculate Title as 'Error'

; Prepare the HTML for the body section of the web page.
Begin text block
Text: <h3>[Title]</h3> (Carriage return,Linefeed)
Text: (Carriage return,Linefeed)
Text: [Mssg] (Carriage return,Linefeed)
End text block
Get text block Body

; Prepare the HTML for a web page that displays the error.
Do $cinst.$retHTMLPageTemplate Returns HTML

Calculate HTML as replaceall(HTML,'##TITLE##',Title)
Calculate HTML as replaceall(HTML,'##BODY##',Body)

Do $cinst.$addHTTPContentHeader(HTML)

Quit method HTML

Fix Remote Task $construct

We can now replace the Breakpoint in the $construct method of rtCountryList with a call to oHTMLTools.

  1. Go to the Breakpoint near the end of the $construct method of rtCountryList.
  2. Replace it with a $retLastErrorHTML message to oHTMLTools. The end of the $construct method should look like this:

    If not(FlagOK)
       
       ; An error occurred. Get the last error as an HTML page.
       Do ioHTMLTools.$retLastErrorHTML() Returns HTML
       
    End If

    Quit method HTML