How to capture session-related errors?

Errors such as

if not caused by incorrect session timeout (Configure the timeout settings) or IIS configuration (Session does not exist), you can capture and show the error in a more friendly way, such as in a message box.

For example, in the Application object -> System Error event

Choose Case error.Number
        Case 220  to 229 //Session Error
                 MessageBox ("Session Error", "Number:" + String(error.Number) + "~r~nText:" + error.Text )
        Case 230  to 239 //License Error
                 MessageBox ("License Error", "Number:" + String(error.Number) + "~r~nText:" + error.Text )
        Case 240  to 249 //Token Error
                 MessageBox ("Token Error", "Number:" + String(error.Number) + "~r~nText:" + error.Text )
        Case Else
                 MessageBox ("SystemError", "Number:" + String(error.Number) + "~r~nText:" + error.Text )
End Choose

You can also replace the default message with your own message for each error. For more details about PowerServer Error Numbers, refer to Runtime error numbers.

For example:

Choose Case error.Number
        Case 224, 225 // Session fails to respond (Session does not exist/Session timed out).
                 MessageBox ("Session Error", "Number:" + String(error.Number) + "~r~nText:" + "Since the application has not been operated for a long time, please restart the application to continue using the application.")
…
End Choose

For each specific type of error, developers can implement proactive approaches and solutions. For example, in a situation where an error occurs due to the primary server not functioning, developers can utilize the SetPowerServerURL and BeginSession functions to smoothly transition to a backup server, so after the end user restarts the application they can continue using the application (thereby indirectly implementing the server failover functionality). Here is a code example:

String ls_new_url
Blob lb_has_error
Try
  GetApplication().BeginSession()
Catch (runtimeerror er)
  // BeginSession failed if the primary server is down.
  lb_has_error = true
  Messagebox("BeginSession Failed", "It will try another service.")
End Try

If lb_has_error Then // It will try the backup server.
  // Change to the backup Web API URL.
  ls_new_url = "https://www.example.com/salesapibackup"
  GetApplication().SetPowerServerURL(ls_new_url)
  GetApplication().BeginSession()
End If