B4J Question [ABMaterial] Catching an Exception?

MathiasM

Active Member
Licensed User
Hello

I do something like this:
-User fills in credentials and presses login button
-An API method is called with the credentials and returns a token, or an error

If API call is succes:
-The API method extracts the bearer token from json result and returns it

If API call fails:
-The API method extracts the error message from json error (if any, otherwise some generic message), assembles a ExceptionEx with the error message and throws that exception.

In the loginbutton click I want to catch that said Exception and show the message to the user. See code:
B4X:
Public Sub btnlogin_Clicked(Target As String)
  
   Dim loginContainer As ABMContainer = page.Component("containerlogin")
   Dim usernameInput As ABMInput = loginContainer.Component("txtusername")
   Dim passwordInput As ABMInput = loginContainer.Component("txtpassword")
  
   Try
       Wait For(API.PerformLogin(usernameInput.Text, passwordInput.Text)) Complete (bearer As String)
       page.ws.Session.SetAttribute("bearer_token", bearer)
       ABMShared.NavigateToPage(page.ws, page.GetPageID, "../DashboardPage")
   Catch
       usernameInput.Text = ""
       usernameInput.SetFocus
       usernameInput.Refresh
      
       passwordInput.Text = ""
       passwordInput.Refresh
      
       Dim errorLabel As ABMLabel = loginContainer.Component("lblError")
       errorLabel.Text = LastException.Message
       errorLabel.Visibility = ABM.VISIBILITY_ALL
       errorLabel.Refresh
   End Try
End Sub
The problem is, it seems to be impossible to catch this exception. It is always displayed in the logs (so before it reaches my Try)

My guess is that ABMaterial catches this error before I can. But where? Where can I pass it along instead of just displaying it to the logs?

I tried searching for 'LastException' as I hoped I could trace the source, but I'm not getting the results I'm hoping for. (only my own references, and in the ABMupload class, which I don't currently use)

@alwaysbusy I think you're the only one that knows this answer?

Thanks a bunch
 

DonManfred

Expert
Licensed User
assembles a ExceptionEx with the error message and throws that
what about setting the bearer to a value which you can interpret as error?
A bearer token probably have a fixed length. Instead of throwing an Exception you can do checks on the token returned.
 

MathiasM

Active Member
Licensed User
what about setting the bearer to a value which you can interpret as error?
A bearer token probably have a fixed length. Instead of throwing an Exception you can do checks on the token returned.
You're right, I could do that. But that would only work in this specific situation with a string as result.
If I would have an API call 'IsUserAnAdult(username As String), and that returns a True or False, I have no way to parse that True or False if a User doesn't excists.

I can solve it in another way, make a custom return object, with a flag if it's a error or not, and a Map with the result values (or error message)

It will work the same way, but is a little more clean imho.

Thanks for thinking with me guys, this forum has an awesome community!
 

DonManfred

Expert
Licensed User
If I would have an API call 'IsUserAnAdult(username As String), and that returns a True or False, I have no way to parse that True or False if a User doesn't excists.
if you have control over the api: Return an Int from the apicall. 0 no adult, 1 adult, any value <0 error (user does not exist).
 
Last edited:

Cableguy

Expert
Licensed User
Another possible solution, since the error appears in the logs, is to use the errorHandler... I've never used it before, but if ABMaterial is catching it at Browser level, it's worth the try
 
Top