Catch Request/Event handler errors at dispatcher level (#1610)

* Catch Request/Event handler errors at dispatcher level

* Fix tests

* Use Exception overload of SendError

* Fix tests
This commit is contained in:
Charles Gagnon
2022-07-29 17:31:36 -07:00
committed by GitHub
parent 3294a52ad9
commit fd00114a0e
32 changed files with 1326 additions and 1921 deletions

View File

@@ -75,25 +75,19 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Credentials
}
[Test]
public async Task SaveCredentialThrowsIfCredentialIdMissing()
public void SaveCredentialThrowsIfCredentialIdMissing()
{
string errorResponse = null;
var contextMock = RequestContextMocks.Create<bool>(null).AddErrorHandling((msg, code, data) => errorResponse = msg);
await service.HandleSaveCredentialRequest(new Credential(null), contextMock.Object);
TestUtils.VerifyErrorSent(contextMock);
Assert.That(errorResponse, Does.Contain("ArgumentException"));
var contextMock = RequestContextMocks.Create<bool>(null);
// Verify throws with no ID
Assert.That(() => service.HandleSaveCredentialRequest(new Credential(null), contextMock.Object), Throws.ArgumentException);
}
[Test]
public async Task SaveCredentialThrowsIfPasswordMissing()
public void SaveCredentialThrowsIfPasswordMissing()
{
string errorResponse = null;
var contextMock = RequestContextMocks.Create<bool>(null).AddErrorHandling((msg, code, data) => errorResponse = msg);
await service.HandleSaveCredentialRequest(new Credential(CredentialId), contextMock.Object);
TestUtils.VerifyErrorSent(contextMock);
Assert.True(errorResponse.Contains("ArgumentException") || errorResponse.Contains("ArgumentNullException"));
var contextMock = RequestContextMocks.Create<bool>(null);
// Verify throws with no ID
Assert.That(() => service.HandleSaveCredentialRequest(new Credential(CredentialId), contextMock.Object), Throws.ArgumentNullException);
}
[Test]
@@ -193,27 +187,11 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Credentials
}
[Test]
public async Task ReadCredentialThrowsIfCredentialIsNull()
public void ReadCredentialThrowsIfCredentialIsNull()
{
string errorResponse = null;
var contextMock = RequestContextMocks.Create<Credential>(null).AddErrorHandling((msg, code, data) => errorResponse = msg);
// Verify throws on null, and this is sent as an error
await service.HandleReadCredentialRequest(null, contextMock.Object);
TestUtils.VerifyErrorSent(contextMock);
Assert.That(errorResponse, Does.Contain("ArgumentNullException"));
}
[Test]
public async Task ReadCredentialThrowsIfIdMissing()
{
string errorResponse = null;
var contextMock = RequestContextMocks.Create<Credential>(null).AddErrorHandling((msg, code, data) => errorResponse = msg);
var contextMock = RequestContextMocks.Create<Credential>(null);
// Verify throws with no ID
await service.HandleReadCredentialRequest(new Credential(), contextMock.Object);
TestUtils.VerifyErrorSent(contextMock);
Assert.That(errorResponse, Does.Contain("ArgumentException"));
Assert.That(() => service.HandleReadCredentialRequest(new Credential(), contextMock.Object), Throws.ArgumentException);
}
[Test]
@@ -235,15 +213,11 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Credentials
}
[Test]
public async Task DeleteCredentialThrowsIfIdMissing()
public void DeleteCredentialThrowsIfIdMissing()
{
object errorResponse = null;
var contextMock = RequestContextMocks.Create<bool>(null).AddErrorHandling((msg, code, data) => errorResponse = msg);
var contextMock = RequestContextMocks.Create<bool>(null);
// Verify throws with no ID
await service.HandleDeleteCredentialRequest(new Credential(), contextMock.Object);
TestUtils.VerifyErrorSent(contextMock);
Assert.True(((string)errorResponse).Contains("ArgumentException"));
Assert.That(() => service.HandleDeleteCredentialRequest(new Credential(), contextMock.Object), Throws.ArgumentException);
}
[Test]

View File

@@ -26,7 +26,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
#region EditSession Operation Helper Tests
[Test]
public async Task NullOrMissingSessionId([Values(null, "", " \t\n\r", "Does not exist")] string sessionId)
public void NullOrMissingSessionId([Values(null, "", " \t\n\r", "Does not exist")] string sessionId)
{
// Setup:
// ... Create a edit data service
@@ -34,14 +34,10 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
// ... Create a session params that returns the provided session ID
var mockParams = new EditCreateRowParams {OwnerUri = sessionId};
var contextMock = RequestContextMocks.Create<EditDisposeResult>(null);
// If: I ask to perform an action that requires a session
// Then: I should get an error from it
var efv = new EventFlowValidator<EditDisposeResult>()
.AddStandardErrorValidation()
.Complete();
await eds.HandleSessionRequest(mockParams, efv.Object, session => null);
efv.Validate();
Assert.That(() => eds.HandleSessionRequest(mockParams, contextMock.Object, session => null), Throws.Exception);
}
[Test]
@@ -54,14 +50,10 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
// ... Create a session param that returns the common owner uri
var mockParams = new EditCreateRowParams { OwnerUri = Common.OwnerUri };
var contextMock = RequestContextMocks.Create<EditDisposeResult>(null);
// If: I ask to perform an action that requires a session
// Then: I should get an error from it
var efv = new EventFlowValidator<EditDisposeResult>()
.AddStandardErrorValidation()
.Complete();
await eds.HandleSessionRequest(mockParams, efv.Object, s => { throw new Exception(); });
efv.Validate();
Assert.That(() => eds.HandleSessionRequest(mockParams, contextMock.Object, s => { throw new Exception(); }), Throws.Exception);
}
@@ -71,18 +63,15 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
#region Dispose Tests
[Test]
public async Task DisposeNullOrMissingSessionId([Values(null, "", " \t\n\r", "Does not exist")] string sessionId)
public void DisposeNullOrMissingSessionId([Values(null, "", " \t\n\r", "Does not exist")] string sessionId)
{
// Setup: Create a edit data service
var eds = new EditDataService(null, null, null);
// If: I ask to perform an action that requires a session
// Then: I should get an error from it
var efv = new EventFlowValidator<EditDisposeResult>()
.AddStandardErrorValidation()
.Complete();
await eds.HandleDisposeRequest(new EditDisposeParams {OwnerUri = sessionId}, efv.Object);
efv.Validate();
var contextMock = RequestContextMocks.Create<EditDisposeResult>(null);
Assert.That(() => eds.HandleDisposeRequest(new EditDisposeParams { OwnerUri = sessionId }, contextMock.Object), Throws.Exception);
}
[Test]
@@ -293,16 +282,11 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
OwnerUri = ownerUri,
ObjectType = objType
};
// ... And I initialize an edit session with that
var efv = new EventFlowValidator<EditInitializeResult>()
.AddStandardErrorValidation()
.Complete();
await eds.HandleInitializeRequest(initParams, efv.Object);
// Then:
// ... An error event should have been raised
efv.Validate();
var contextMock = RequestContextMocks.Create<EditInitializeResult>(null);
// ... And I initialize an edit session with that
// Then:
// ... An error event should have been sent
Assert.That(() => eds.HandleInitializeRequest(initParams, contextMock.Object), Throws.ArgumentException);
// ... There should not be a session
Assert.That(eds.ActiveSessions, Is.Empty);
@@ -324,16 +308,13 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
ObjectType = "Table",
Filters = new EditInitializeFiltering()
};
var efv = new EventFlowValidator<EditInitializeResult>()
.AddStandardErrorValidation()
.Complete();
await eds.HandleInitializeRequest(initParams, efv.Object);
// Then:
// ... An error event should have been sent
efv.Validate();
// ... The original session should still be there
var contextMock = RequestContextMocks.Create<EditInitializeResult>(null);
// Then:
// ... An error event should have been sent
Assert.That(() => eds.HandleInitializeRequest(initParams, contextMock.Object), Throws.ArgumentNullException);
// ... The original session should still be there
Assert.AreEqual(1, eds.ActiveSessions.Count);
Assert.AreEqual(session, eds.ActiveSessions[Constants.OwnerUri]);
}

View File

@@ -198,11 +198,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution
// ... And I then ask for a valid execution plan from it
var executionPlanParams = new QueryExecutionPlanParams { OwnerUri = Constants.OwnerUri, ResultSetIndex = 0, BatchIndex = 0 };
var executionPlanRequest = new EventFlowValidator<QueryExecutionPlanResult>()
.AddStandardErrorValidation()
.Complete();
await queryService.HandleExecutionPlanRequest(executionPlanParams, executionPlanRequest.Object);
executionPlanRequest.Validate();
var contextMock = RequestContextMocks.Create<QueryExecutionPlanResult>(null);
Assert.That(() => queryService.HandleExecutionPlanRequest(executionPlanParams, contextMock.Object), Throws.InvalidOperationException);
}
[Test]
@@ -229,11 +226,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution
// ... And I then ask for an execution plan from a result set
var executionPlanParams = new QueryExecutionPlanParams { OwnerUri = Constants.OwnerUri, ResultSetIndex = 0, BatchIndex = 0 };
var executionPlanRequest = new EventFlowValidator<QueryExecutionPlanResult>()
.AddStandardErrorValidation()
.Complete();
await queryService.HandleExecutionPlanRequest(executionPlanParams, executionPlanRequest.Object);
executionPlanRequest.Validate();
var contextMock = RequestContextMocks.Create<QueryExecutionPlanResult>(null);
Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => queryService.HandleExecutionPlanRequest(executionPlanParams, contextMock.Object));
}
#endregion

View File

@@ -163,11 +163,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution
var workspaceService = Common.GetPrimedWorkspaceService(Constants.StandardQuery);
var queryService = Common.GetPrimedExecutionService(null, true, false, false, workspaceService);
var subsetParams = new SubsetParams { OwnerUri = Constants.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0 };
var subsetRequest = new EventFlowValidator<SubsetResult>()
.AddStandardErrorValidation()
.Complete();
await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);
subsetRequest.Validate();
var contextMock = RequestContextMocks.Create<SubsetResult>(null);
Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => queryService.HandleResultSubsetRequest(subsetParams, contextMock.Object));
}
[Test]
@@ -186,11 +183,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution
// ... And I then ask for a valid set of results from it
var subsetParams = new SubsetParams { OwnerUri = Constants.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0 };
var subsetRequest = new EventFlowValidator<SubsetResult>()
.AddStandardErrorValidation()
.Complete();
await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);
subsetRequest.Validate();
var contextMock = RequestContextMocks.Create<SubsetResult>(null);
Assert.That(() => queryService.HandleResultSubsetRequest(subsetParams, contextMock.Object), Throws.InvalidOperationException);
}
[Test]
@@ -208,11 +202,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution
// ... And I then ask for a set of results from it
var subsetParams = new SubsetParams { OwnerUri = Constants.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0 };
var subsetRequest = new EventFlowValidator<SubsetResult>()
.AddStandardErrorValidation()
.Complete();
await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);
subsetRequest.Validate();
var contextMock = RequestContextMocks.Create<SubsetResult>(null);
Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => queryService.HandleResultSubsetRequest(subsetParams, contextMock.Object));
}
#endregion