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

@@ -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]);
}