benibela/internettools

Removing Or enhance the EInternetException

Coldzer0 opened this issue · 2 comments

Hello

I was working on a project using your library and I found about that there's no way of retrieving the response body of an 400+ messages.

I only got EInternetException exception with error code and which link and that's it.

The body of the request in any situation is important because sometimes the body contains some detailed error messages etc..

at this line
https://github.com/benibela/internettools/blob/master/internet/internetaccess.pas#L1097

      else begin
        message := IntToStr(transfer.HTTPResultCode) + ' ' + transfer.HTTPErrorDetails;
        if transfer.HTTPResultCode <= 0 then message := 'Internet Error: ' + message
        else message := 'Internet/HTTP Error: ' + message;
        raise EInternetException.Create(message + LineEnding + 'when talking to: '+url.combined, transfer.HTTPResultCode);
      end;  

so I think the right thing todo there is either
1- Add the data of the body to the EInternetException
2- Remove the Exception and let the user decide by the lastHTTPResultCode (this is what I do in my project)

In case of the full project of yours I think adding a body variable to EInternetException and assign the response body.

When it raises the exception, it does not have the data. It always download everything and writes it to the destination, but that is a write-only callback. And if it has downloaded a few GB data, it could not put them in memory.

So, with a stream, it always writes the data in the stream, before the exception:

fs := TFileStream.Create('/tmp/out.txt', fmCreate);
internet.get('https://example.org/tmpxxxxxxxxx', fs);

The OnTransferReact event is supposed to be used to disable exceptions

Thank you for the explanation