AL icon indicating copy to clipboard operation
AL copied to clipboard

TryFunction with error('') in base application causes runtime error although it is handled with if and then;

Open auzhhv opened this issue 2 years ago • 0 comments

I apologize in advance if this is not the correct place to report this. Note that I am not reporting a bug in the Base Application, but I am reporting behavior that is not expected by code in the Base Application that we are using in our customization.

1. Describe the bug We have some custom code that runs in a job queue that uses the "Error Message Management" codeunit in Base Application to log errors. It usually works fine. But sometimes an error is thrown with the following message: "Duplicate of identifier 2005865" (the number changes). The first line in procedure GetCurrCallStack in codeunit "Error Message Management" runs a TryFunction that only contains Error(''). And the procedure is handled with if ThrowError then; So why does this sometimes cause a runtime error that makes to job queue stop? This should not cause the job queue to stop, and I'm not able to find this error message in Base Application or System Application, and not in our custom code, which makes me think that it might be a bug. View call stack below.

2. To Reproduce Steps to reproduce the behavior:

When the job queue starts running it runs the ActivateErrorLog procedure once. If an error occurs it runs the LogError procedure. The first one activates the error message functionality in Base Applicaation, the second procedure logs the error. This usually works fine, but sometimes is stops the job queue and give us the strange error.

    procedure ActivateErrorLog()
    begin
        Clear(ErrorMessageHandler);
        Clear(ErrorMessageManagement);
        Clear(ErrorContextElement);
        ErrorMessageManagement.Activate(ErrorMessageHandler);
        ErrorLogActive := true;
    end;

    procedure LogError(ContectVariant: Variant; AdditionalInfo: Text)
    begin
        if ErrorLogActive then begin
            ErrorMessageManagement.PushContext(ErrorContextElement, ContectVariant, 0, CopyStr(AdditionalInfo, 1, 250)); //TODO: sjekk om dette økes i fremtidige versjoner
            ErrorMessageManagement.LogSimpleErrorMessage(GetLastErrorText);
        end;
    end;

    var
        ErrorMessageManagement: Codeunit "Error Message Management";
        ErrorContextElement: Codeunit "Error Context Element";
        ErrorMessageHandler: Codeunit "Error Message Handler";
        ErrorLogActive: Boolean;

3. Expected behavior

This code should not make the job queue stop with an error as the ThrowError procedure is a TryFunction and it is handled with if and then.

    procedure GetCurrCallStack() CallStack: Text;
    var
        Len: Integer;
        Pos: Integer;
        SubString: Text;
    begin
        if ThrowError() then;
        CallStack := GetLastErrorCallStack();
        SubString := '"Error Message Management"(CodeUnit 28)';
        Len := StrLen(SubString);
        repeat
            Pos := StrPos(CallStack, SubString);
            CallStack := CopyStr(CallStack, Pos + Len);
        until Pos = 0;
        CallStack := CopyStr(CallStack, StrPos(CallStack, '\') + 1);
    end;

    [TryFunction]
    local procedure ThrowError()
    begin
        // Throw an error to get the call stack by GetLastErrorCallstack
        Error('');
    end;

4. Actual behavior Here is the callstack..

Duplicate of identifier 2005865

"Error Message Management"(CodeUnit 28).ThrowError line 3 - Base Application by Microsoft "Error Message Management"(CodeUnit 28).GetCurrCallStack line 6 - Base Application by Microsoft "Error Message"(Table 700).LogSimpleMessage line 23 - Base Application by Microsoft "Error Message"(Table 700).LogSimpleMessage line 2 - Base Application by Microsoft "Error Message Handler"(CodeUnit 29).OnLogSimpleErrorHandler line 3 - Base Application by Microsoft "Error Message Management"(CodeUnit 28).OnLogSimpleError(Event) line 2 - Base Application by Microsoft "Error Message Management"(CodeUnit 28).LogSimpleErrorMessage line 2 - Base Application by Microsoft "ADI Distribution Management"(CodeUnit 70900).LogError line 4 - Auzilium Document Distribution by Auzilium "ADI Distribute Sales Cr. Memos"(CodeUnit 70902).DoDistributeDocument line 7 - Auzilium Document Distribution by Auzilium "ADI Distribute Sales Cr. Memos"(CodeUnit 70902).DistributeDocuments line 22 - Auzilium Document Distribution by Auzilium "ADI Distribute Pstd. S. Cr.M."(Report 70901)."SalesCrMemoHeader - OnPreDataItem"(Trigger) line 5 - Auzilium Document Distribution by Auzilium "Job Queue Start Report"(CodeUnit 487).RunReport line 32 - Base Application by Microsoft "Job Queue Start Report"(CodeUnit 487).OnRun(Trigger) line 2 - Base Application by Microsoft "Job Queue Start Codeunit"(CodeUnit 449).RunReport line 9 - Base Application by Microsoft "Job Queue Start Codeunit"(CodeUnit 449).OnRun(Trigger) line 19 - Base Application by Microsoft "Job Queue Dispatcher"(CodeUnit 448).HandleRequest line 28 - Base Application by Microsoft "Job Queue Dispatcher"(CodeUnit 448).OnRun(Trigger) line 19 - Base Application by Microsoft

5. Versions:

  • AL Language: Version 12.0
  • Visual Studio Code: 1.82.2
  • Business Central: NO Business Central 22.5 (plattform 22.0.60723.0 + program 22.5.59966.60134)

auzhhv avatar Oct 02 '23 18:10 auzhhv