// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Microsoft.Teams.AI.Prompts;

public partial class ChatPrompt<TOptions>
{
    public IChatPrompt<TOptions> OnError(Action<Exception> onError)
    {
        ErrorEvent += (_, ex) => onError(ex);
        return this;
    }

    public IChatPrompt<TOptions> OnError(Func<Exception, Task> onError)
    {
        ErrorEvent += (_, ex) =>
        {
            _ = onError(ex).ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    Logger.Error(t.Exception);
                }
            }, TaskScheduler.Default);
        };
        return this;
    }
}
