// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Common.Search;
///
/// Represents an error that occurred during a search operation.
///
public sealed class SearchError
{
///
/// Initializes a new instance of the class.
///
/// The error code.
/// The error message.
/// Optional additional details.
/// Optional exception that caused the error.
public SearchError(SearchErrorCode code, string message, string? details = null, Exception? exception = null)
{
Code = code;
Message = message;
Details = details;
Exception = exception;
Timestamp = DateTime.UtcNow;
}
///
/// Gets the error code.
///
public SearchErrorCode Code { get; }
///
/// Gets the error message.
///
public string Message { get; }
///
/// Gets additional details about the error.
///
public string? Details { get; }
///
/// Gets the exception that caused the error, if any.
///
public Exception? Exception { get; }
///
/// Gets the timestamp when the error occurred.
///
public DateTime Timestamp { get; }
///
/// Creates an error for initialization failure.
///
/// The name of the index.
/// Optional details.
/// Optional exception.
/// A new SearchError instance.
public static SearchError InitializationFailed(string indexName, string? details = null, Exception? exception = null)
=> new(SearchErrorCode.InitializationFailed, $"Failed to initialize search index '{indexName}'.", details, exception);
///
/// Creates an error for indexing failure.
///
/// The ID of the content that failed to index.
/// Optional details.
/// Optional exception.
/// A new SearchError instance.
public static SearchError IndexingFailed(string contentId, string? details = null, Exception? exception = null)
=> new(SearchErrorCode.IndexingFailed, $"Failed to index content '{contentId}'.", details, exception);
///
/// Creates an error for search query failure.
///
/// The search query that failed.
/// Optional details.
/// Optional exception.
/// A new SearchError instance.
public static SearchError SearchFailed(string query, string? details = null, Exception? exception = null)
=> new(SearchErrorCode.SearchFailed, $"Search query '{query}' failed.", details, exception);
///
/// Creates an error for engine not ready.
///
/// The operation that was attempted.
/// A new SearchError instance.
public static SearchError EngineNotReady(string operation)
=> new(SearchErrorCode.EngineNotReady, $"Search engine is not ready. Operation '{operation}' cannot be performed.");
///
/// Creates an error for capability unavailable.
///
/// The capability that is unavailable.
/// Optional details.
/// A new SearchError instance.
public static SearchError CapabilityUnavailable(string capability, string? details = null)
=> new(SearchErrorCode.CapabilityUnavailable, $"Search capability '{capability}' is not available.", details);
///
/// Creates an error for timeout.
///
/// The operation that timed out.
/// The timeout duration.
/// A new SearchError instance.
public static SearchError Timeout(string operation, TimeSpan timeout)
=> new(SearchErrorCode.Timeout, $"Operation '{operation}' timed out after {timeout.TotalSeconds:F1} seconds.");
///
/// Creates an error for an unexpected error.
///
/// The operation that failed.
/// The exception that occurred.
/// A new SearchError instance.
public static SearchError Unexpected(string operation, Exception exception)
=> new(SearchErrorCode.Unexpected, $"Unexpected error during '{operation}'.", exception.Message, exception);
///
public override string ToString()
{
var result = $"[{Code}] {Message}";
if (!string.IsNullOrEmpty(Details))
{
result += $" Details: {Details}";
}
if (Exception != null)
{
result += $" Exception: {Exception.GetType().Name}: {Exception.Message}";
}
return result;
}
}