I must be overlooking something....
Why doesn't the NetHTTPClientRequestError
get triggered here?
type
TUpdateChecker = class
private
var FHTTP : TNetHTTPClient;
var FAbort : Boolean;
procedure NetHTTPClientRequestError(const Sender: TObject; const AError: string);
public
constructor Create;
destructor Destroy; override;
function CheckNewVersion(var ANewVer: String): Boolean;
end;
{ TUpdateChecker }
constructor TUpdateChecker.Create;
begin
inherited;
FHTTP := TNetHTTPClient.Create(nil);
FHTTP.OnRequestError := NetHTTPClientRequestError; // Error handler assigned here
FHTTP.ConnectionTimeOut := 10000;
end;
destructor TUpdateChecker.Destroy;
begin
FHTTP.Free;
inherited;
end;
function TUpdateChecker.CheckNewVersion(var ANewVer: String): Boolean;
var
lJSONStr: String;
begin
Result := false;
try
lJSONStr := FHTTP.Get(cUpdateJSON + 'sdhgfy').ContentAsString; // Add some junk to cause error
except
on E:Exception do
begin
// We don't get at the breakpoint here
WriteErrorLog(TSettings.DataLocation + cErrLogFile, 'CheckNewVersion: ' + E.Message);
Exit;
end;
end;
if FAbort then Exit; // FABort = false here
// Rest of code removed
end;
procedure TUpdateChecker.NetHTTPClientRequestError(const Sender: TObject; const AError: string);
begin
// We don't get at the breakpoint here, FAbort never becomes true
ShowMessage('ERROR Sender.ClassName=' + Sender.ClassName + ' AError=' + AError);
FAbort := true;
end;
Calling code from a FormShow:
lCheck := TUpdateChecker.Create;
try
if lCheck.CheckNewVersion(lNewVer) then
begin
LblNewVersion.Caption := Format(LblNewVersion.Caption,[lNewVer]);
LblNewVersion.Visible := true;
LblUpgrade.Visible := true;
end;
finally
lCheck.Free;
end;
This is a Win32 app running on Win10.
cUpdateJSON
is a valid URL to a JSON file on my website. I added the 'sdhgfy' junk to cause the error. My attention is to catch both 'common' HTTP status code like 500, 404, as well as exceptions.