TFPHttpClient exception 404 lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- dadycool · 2022-04-01 00:13 Hi All I want to handle http response 404 myself but a standard exception message box still appear. How to get rid of it? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ... uses    Classes, SysUtils, Forms, Controls, Graphics, Dialogs,       StdCtrls, fphttpclient, opensslsockets, openssl; ... procedure TForm1.Button2Click(Sender: TObject);var   Client: TFPHttpClient;begin   InitSSLInterface;   Client:= TFPHttpClient.Create(nil);  try           Client.Get('https://pastebin.com/ettUp08f'); //404           ShowMessage(IntToStr(Client.ResponseStatusCode));  finally    Client.Free;  end;end;  Thanks! ---------------------------------------------------------------------- PierceNg · 2022-04-01 03:05 Quote from: dadycool on April 01, 2022, 02:13:17 amI want to handle http response 404 myself but a standard exception message box still appear. How to get rid of it? Use try-except inside try-finally. You could be more precise and handle the specific exception(s) raised by TFPHttpClient. See wiki page. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program fourohfour; {$mode objfpc} uses    classes, sysutils, fphttpclient, opensslsockets, openssl; var    cli: TFPHttpClient; begin    InitSSLInterface;    cli := TFPHttpClient.Create(nil);    try        try            cli.Get('https://pastebin.com/ettUp08f');            writeln('Response: ', IntToStr(cli.ResponseStatusCode));        except            on e: Exception do               begin              writeln('Exception: ', e.Message);                writeln('HTTP client response: ', cli.ResponseStatusCode);              end;        end;    finally        cli.Free;    end;end.  Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i % fpc 404.pasFree Pascal Compiler version 3.2.2 [2022/01/13] for x86_64Copyright (c) 1993-2021 by Florian Klaempfl and othersTarget OS: Linux for x86-64Compiling 404.pasLinking 40428 lines compiled, 0.3 sec % ./404Exception: Unexpected response status code: 404HTTP client response: 404 ---------------------------------------------------------------------- dbannon · 2022-04-01 06:41 Here is a bit of code from my app.  Don't forget, when running under the debugger (ie in the IDE) you will always get told about the exception, even if handled in your code. So, test without the debugger .... The SayDebugSafe() is a logger of mine, it always returns false .. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     try      try            SomeString := Client.Get(URL);        except          on E: ESocketError do begin                ErrorString := 'Github.Downloader ' + E.Message;                exit(SayDebugSafe(ErrorString));                end;            on E: EInOutError do begin                ErrorString := 'Github Downloader - InOutError ' + E.Message;                exit(SayDebugSafe(ErrorString));                end;            on E: ESSL do begin                ErrorString := 'Github.Downloader -SSLError ' + E.Message;                exit(SayDebugSafe(ErrorString));                end;            on E: Exception do begin                ErrorString := 'GitHub.Downloader Exception ' + E.Message + ' downloading ' + URL;                case Client.ResponseStatusCode of                    401 : ErrorString := ErrorString + ' 401 Maybe your Token has expired or password is invalid ??';                    404 : ErrorString := ErrorString + ' 404 File not found ' + URL;                end;                exit(SayDebugSafe(ErrorString));                end;        end; ---------------------------------------------------------------------- dadycool · 2022-04-01 14:43 Quote from: PierceNg on April 01, 2022, 05:05:42 amQuote from: dadycool on April 01, 2022, 02:13:17 amI want to handle http response 404 myself but a standard exception message box still appear. How to get rid of it? Use try-except inside try-finally. You could be more precise and handle the specific exception(s) raised by TFPHttpClient. See wiki page. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program fourohfour; {$mode objfpc} uses    classes, sysutils, fphttpclient, opensslsockets, openssl; var    cli: TFPHttpClient; begin    InitSSLInterface;    cli := TFPHttpClient.Create(nil);    try        try            cli.Get('https://pastebin.com/ettUp08f');            writeln('Response: ', IntToStr(cli.ResponseStatusCode));        except            on e: Exception do               begin              writeln('Exception: ', e.Message);                writeln('HTTP client response: ', cli.ResponseStatusCode);              end;        end;    finally        cli.Free;    end;end.  Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i % fpc 404.pasFree Pascal Compiler version 3.2.2 [2022/01/13] for x86_64Copyright (c) 1993-2021 by Florian Klaempfl and othersTarget OS: Linux for x86-64Compiling 404.pasLinking 40428 lines compiled, 0.3 sec % ./404Exception: Unexpected response status code: 404HTTP client response: 404  This work for me without using debugger. Thanks a ton, PierceNg!! Quote from: dbannon on April 01, 2022, 08:41:44 amHere is a bit of code from my app.  Don't forget, when running under the debugger (ie in the IDE) you will always get told about the exception, even if handled in your code. So, test without the debugger .... The SayDebugSafe() is a logger of mine, it always returns false .. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     try      try            SomeString := Client.Get(URL);        except          on E: ESocketError do begin                ErrorString := 'Github.Downloader ' + E.Message;                exit(SayDebugSafe(ErrorString));                end;            on E: EInOutError do begin                ErrorString := 'Github Downloader - InOutError ' + E.Message;                exit(SayDebugSafe(ErrorString));                end;            on E: ESSL do begin                ErrorString := 'Github.Downloader -SSLError ' + E.Message;                exit(SayDebugSafe(ErrorString));                end;            on E: Exception do begin                ErrorString := 'GitHub.Downloader Exception ' + E.Message + ' downloading ' + URL;                case Client.ResponseStatusCode of                    401 : ErrorString := ErrorString + ' 401 Maybe your Token has expired or password is invalid ??';                    404 : ErrorString := ErrorString + ' 404 File not found ' + URL;                end;                exit(SayDebugSafe(ErrorString));                end;        end; This is very useful. Thanks dbannon!