Add error codes for password change (reopened) (#1784)

* added error code handling

* Small cleanup

* added changes
This commit is contained in:
Alex Ma
2022-12-13 13:04:40 -08:00
committed by GitHub
parent cc94a1a4c6
commit 548229d547
5 changed files with 19 additions and 51 deletions

View File

@@ -22,7 +22,6 @@ using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Microsoft.SqlTools.Utility;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace Microsoft.SqlTools.ServiceLayer.Connection
{
@@ -38,6 +37,12 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
public const int MaxServerlessReconnectTries = 5; // Max number of tries to wait for a serverless database to start up when its paused before giving up.
// SQL Error Code Constants
// Referenced from: https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors?view=sql-server-ver16
private const int DoesNotMeetPWReqs = 18466; // Password does not meet complexity requirements.
private const int PWCannotBeUsed = 18463; // Password cannot be used at this time.
/// <summary>
/// Singleton service instance
/// </summary>
@@ -1159,18 +1164,25 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
catch (Exception ex)
{
newResponse.Result = false;
newResponse.ErrorMessage = ex.InnerException != null ? (ex.Message + Environment.NewLine + Environment.NewLine + ex.InnerException.Message) : ex.Message;
newResponse.ErrorMessage = Regex.Replace(newResponse.ErrorMessage, @"\r?\nChanged database context to '\w+'\.", "");
newResponse.ErrorMessage = Regex.Replace(newResponse.ErrorMessage, @"\r?\nChanged language setting to \w+\.", "");
if (newResponse.ErrorMessage.Equals(SR.PasswordChangeEmptyPassword))
newResponse.ErrorMessage = ex.Message;
int errorCode = 0;
if ((ex.InnerException as SqlException) != null && (ex.InnerException as SqlException)?.Errors.Count != 0)
{
SqlError endError = (ex.InnerException as SqlException).Errors[0];
newResponse.ErrorMessage = endError.Message;
errorCode = endError.Number;
}
if (errorCode == 0 && newResponse.ErrorMessage.Equals(SR.PasswordChangeEmptyPassword))
{
newResponse.ErrorMessage += Environment.NewLine + Environment.NewLine + SR.PasswordChangeEmptyPasswordRetry;
}
else if (newResponse.ErrorMessage.Contains(SR.PasswordChangeDNMReqs))
else if (errorCode == DoesNotMeetPWReqs)
{
newResponse.ErrorMessage += Environment.NewLine + Environment.NewLine + SR.PasswordChangeDNMReqsRetry;
}
else if (newResponse.ErrorMessage.Contains(SR.PasswordChangePWCannotBeUsed))
else if (errorCode == PWCannotBeUsed)
{
newResponse.ErrorMessage += Environment.NewLine + Environment.NewLine + SR.PasswordChangePWCannotBeUsedRetry;
}