Catch EXDataHttpUnauthorized.Create('Invalid user credentials.') in webclient

Hi,

How can I catch an exception from XData and get the message in a Webcore application?

Call XData Login service in Webcore application

LResp := await( Client.RawInvokeAsync( 'ILoginService.Login',
[ Login, Password ] ) );

XData Login service in XData
When logins fails the following exception will be raised.

raise EXDataHttpUnauthorized.Create('Invalid user credentials.');

How can I handle the exception in Webcore (silently) and show the error message?

You can simply wrap the call in a try..except block, for example:

procedure TForm1.InvokeGet;
var
  Response: TXDataClientResponse;
begin
  try
    Response := await(XDataWebClient1.GetAsync(SelectedEntitySet, '$filter=Bla eq 2', SelectedId));
  except
    on E: EXDataClientRequestException do
      ShowMessage('Error ' + E.Message + ' with status code ' + IntToStr(E.ErrorResult.StatusCode));
  end;
  GetMemo.Lines.Text := TJSJson.stringify(Response.Result)
end;

Alternatively you can use TXDataWebClient.OnError event as well:

procedure TForm1.XDataWebClient1Error(Error: TXDataClientError);
begin
  ShowInfo(
    Format('%s. RequestId: %s. Code: %s. Request Url: %s',
      [Error.ErrorMessage, Error.RequestId, Error.ErrorCode, Error.RequestUrl]),
    True
  );
end;

The concept is illustrated in the demo at \demos\web\clientAsync\XDataWebClientAsyncDemo.dproj

Perfect. I am now getting the required results.

Thanks for the quick response and example.

1 Like

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