Skip to content

Commit 014f639

Browse files
fix: if exeption then shutdown the console
The issue is when console mode a exception occure the program does not terminate in test unit. A GUI popup form will be shown.
1 parent 7a082b3 commit 014f639

File tree

1 file changed

+84
-66
lines changed

1 file changed

+84
-66
lines changed

source/code/main.pas

Lines changed: 84 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -695,89 +695,107 @@ procedure TFormTrackerModify.ConsoleMode;
695695
//there must be 2 command line items for console mode
696696
FConcoleMode := ParamCount = 2;
697697

698-
if FConcoleMode then
699-
begin
700-
//Create the log file. The old one will be overwritten
701-
AssignFile(FLogFile, ExtractFilePath(Application.ExeName) + LOG_FILE_NAME);
702-
ReWrite(FLogFile);
703-
end;
698+
try
699+
if FConcoleMode then
700+
begin
701+
//Create the log file. The old one will be overwritten
702+
AssignFile(FLogFile, ExtractFilePath(Application.ExeName) + LOG_FILE_NAME);
703+
ReWrite(FLogFile);
704+
end;
704705

705-
//Get the startup command lime parameters.
706-
//ConsoleModeDecodeParameter(FileNameOrDirStr, FTrackerList);
706+
//Get the startup command lime parameters.
707+
//ConsoleModeDecodeParameter(FileNameOrDirStr, FTrackerList);
707708

708-
//If FTrackerList.LogStringList empty then there is no error.
709-
// if FTrackerList.LogStringList.Text = '' then
710-
if ConsoleModeDecodeParameter(FileNameOrDirStr, FTrackerList) then
711-
begin
712-
//There is no error. Proceed with reading the torrent files
709+
//If FTrackerList.LogStringList empty then there is no error.
710+
// if FTrackerList.LogStringList.Text = '' then
711+
if ConsoleModeDecodeParameter(FileNameOrDirStr, FTrackerList) then
712+
begin
713+
//There is no error. Proceed with reading the torrent files
713714

714-
if ExtractFileExt(FileNameOrDirStr) = '' then
715-
begin //There is no file extention. It must be a folder.
716-
if LoadTorrentViaDir(FileNameOrDirStr) then
717-
begin
718-
//Show all the tracker inside the torrent files.
719-
ShowTrackerInsideFileList;
720-
//Mark all trackers as selected
721-
CheckedOnOffAllTrackers(True);
722-
//Some tracker must be removed. Console and windows mode.
723-
UpdateViewRemoveTracker;
724-
725-
if FConcoleMode then
715+
if ExtractFileExt(FileNameOrDirStr) = '' then
716+
begin //There is no file extention. It must be a folder.
717+
if LoadTorrentViaDir(FileNameOrDirStr) then
726718
begin
727-
//update torrent
728-
UpdateTorrent;
729-
end;
719+
//Show all the tracker inside the torrent files.
720+
ShowTrackerInsideFileList;
721+
//Mark all trackers as selected
722+
CheckedOnOffAllTrackers(True);
723+
//Some tracker must be removed. Console and windows mode.
724+
UpdateViewRemoveTracker;
725+
726+
if FConcoleMode then
727+
begin
728+
//update torrent
729+
UpdateTorrent;
730+
end;
730731

731-
end;
732-
end
733-
else //a torrent file is selected?
734-
begin
735-
if ExtractFileExt(FileNameOrDirStr) = '.torrent' then
732+
end;
733+
end
734+
else //a torrent file is selected?
736735
begin
737-
StringList := TStringList.Create;
738-
try
739-
//Convert Filenames to stringlist format.
740-
StringList.Add(FileNameOrDirStr);
736+
if ExtractFileExt(FileNameOrDirStr) = '.torrent' then
737+
begin
738+
StringList := TStringList.Create;
739+
try
740+
//Convert Filenames to stringlist format.
741+
StringList.Add(FileNameOrDirStr);
741742

742-
//Extract all the trackers inside the torrent file
743-
if AddTorrentFileList(StringList) then
744-
begin
745-
//Show all the tracker inside the torrent files.
746-
ShowTrackerInsideFileList;
747-
//Mark all trackers as selected
748-
CheckedOnOffAllTrackers(True);
749-
//Some tracker must be removed. Console and windows mode.
750-
UpdateViewRemoveTracker;
751-
752-
if FConcoleMode then
743+
//Extract all the trackers inside the torrent file
744+
if AddTorrentFileList(StringList) then
753745
begin
754-
//update torrent
755-
UpdateTorrent;
746+
//Show all the tracker inside the torrent files.
747+
ShowTrackerInsideFileList;
748+
//Mark all trackers as selected
749+
CheckedOnOffAllTrackers(True);
750+
//Some tracker must be removed. Console and windows mode.
751+
UpdateViewRemoveTracker;
752+
753+
if FConcoleMode then
754+
begin
755+
//update torrent
756+
UpdateTorrent;
757+
end;
758+
756759
end;
757760

761+
finally
762+
StringList.Free;
758763
end;
759-
760-
finally
761-
StringList.Free;
764+
end
765+
else
766+
begin //Error. this is not a torrent file
767+
FTrackerList.LogStringList.Add('ERROR: No torrent file selected.');
762768
end;
763-
end
764-
else
765-
begin //Error. this is not a torrent file
766-
FTrackerList.LogStringList.Add('ERROR: No torrent file selected.');
767769
end;
768770
end;
769-
end;
770771

771-
if FConcoleMode then
772-
begin
773-
//Write to log file. And close the file.
774-
WriteLn(FLogFile, FTrackerList.LogStringList.Text);
775-
CloseFile(FLogFile);
772+
if FConcoleMode then
773+
begin
774+
//Write to log file. And close the file.
775+
WriteLn(FLogFile, FTrackerList.LogStringList.Text);
776+
CloseFile(FLogFile);
776777

777-
//Shutdown the console program
778-
Application.terminate;
779-
end;
778+
//Shutdown the console program
779+
Application.terminate;
780+
end;
780781

782+
783+
except
784+
if FConcoleMode then
785+
begin
786+
//Shutdown the console program.
787+
//This is needed or else the program will keep running forever.
788+
//exit with error code
789+
System.ExitCode := 1;
790+
Application.terminate;
791+
end
792+
else
793+
begin
794+
//show to the end user that something is wrong
795+
raise;
796+
end;
797+
798+
end;
781799
end
782800
else
783801
begin //the program

0 commit comments

Comments
 (0)