//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
namespace Microsoft.SqlTools.ResourceProvider.Core
{
///
/// Extension methods and utils for exceptions
///
internal static class ExceptionUtil
{
///
/// Returns true if given exception if any of the inner exceptions is UserNeedsAuthenticationException
///
internal static bool IsUserNeedsReauthenticateException(this Exception ex)
{
return ex.IsExceptionType(typeof(UserNeedsAuthenticationException));
}
///
/// Returns true if given exception if any of the inner exceptions is sql exception
///
internal static bool IsDbException(this Exception ex)
{
return ex.IsExceptionType(typeof (DbException));
}
///
/// Returns true if given exception if any of the inner exceptions is same type of given type
///
internal static bool IsExceptionType(this Exception ex, Type type)
{
if (ex == null)
{
return false;
}
if (ex is AggregateException)
{
var aggregateException = (AggregateException)ex;
return aggregateException.InnerExceptions != null &&
aggregateException.InnerExceptions.Any(inner => inner.IsExceptionType(type));
}
else if (type.IsAssignableFrom(ex.GetType()) || (ex.InnerException != null && ex.InnerException.IsExceptionType(type)))
{
return true;
}
return false;
}
internal static string GetExceptionMessage(this Exception ex)
{
string errorMessage = string.Empty;
if (ex != null)
{
errorMessage = ex.Message;
if (ex.InnerException != null)
{
errorMessage += " " + ex.InnerException.Message;
}
}
return errorMessage;
}
}
}