TWebHttpRequest error handling

Hi,

In my code I have the following routine:

class function TService.PerformServiceGetRequest(const ServicePath:String):TJSXMLHttpRequest;
var
  wr:TWebHttpRequest;
begin
  try
    try
      wr := TWebHttpRequest.Create(nil);
      wr.Command := thttpcommand.httpGET;
      wr.URL :=  Format('%s/%s',
                        [TDatabase.GetServerAddress, ServicePath]
                        );
      wr.Headers.Clear;
      wr.Headers.AddPair('content-type', 'application/json');
      wr.User:=TDatabase.GetUser;
      wr.Password:=TDatabase.GetPassword;
      Result:=(await(TJSXMLHttpRequest, wr.Perform));
    except
      on E:Exception do
        Result:=nil
    end;
  finally
    wr.Free;
  end;

end;

I have encountered a point in my program where an error occurs.
Having walked through with the debugger, it gets to THttpRequest.HandleError, and an exception is raised.

Now, the reason for why the error occurs is on my end; however, what I don't understand is why the exception is not caught, as I have a try-catch around the web request as seen in the above.

Am I doing error handling incorrectly or is this a bug?

Thanks!

It is certainly related to the error happening from a promise based call (i.e. the await used here) and the browser internally offloads this to a separate thread in which the error happens then.
To map the JavaScript error happening in this promise to a Pascal exception in the same thread is far from trivial and we’ll need to take this up with the pas2js team. So, the issue is not in your code but it is just a technically very complex scenario to handle this in a classic Pascal exception handling way.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.