-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] Implement exception handler service; fixes #345
* Implement new logic * Bump version number to 27.1.0 * Finetune property to public * Add concept of Payload for IExceptionHandler * Implement handling CDPErrorTag http header * Add and impement IHaveCDPErrorTag interface * Refactor CDP Error Tag to follow existing flows * Add unit tests and some small refactoring based on that * Remove unwanted backup project file
- Loading branch information
1 parent
13c0753
commit 1e4ff16
Showing
24 changed files
with
609 additions
and
45 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
CDP4Common.NetCore.Tests/ExceptionHandlerService/ExceptionHandlerServiceTestFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright file="ExceptionHandlerServiceTestFixture.cs" company="Starion Group S.A."> | ||
// Copyright (c) 2015-2024 Starion Group S.A. | ||
// | ||
// Author: Sam Gerené, Merlin Bieze, Alex Vorobiev, Naron Phou, Alexander van Delft | ||
// | ||
// This file is part of CDP4-COMET-SDK Community Edition | ||
// | ||
// The CDP4-COMET-SDK Community Edition is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 3 of the License, or (at your option) any later version. | ||
// | ||
// The CDP4-COMET-SDK Community Edition is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with this program; if not, write to the Free Software Foundation, | ||
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
namespace CDP4Common.Tests.Helpers | ||
{ | ||
using System; | ||
|
||
using NUnit.Framework; | ||
|
||
using ExceptionHandlerService; | ||
|
||
/// <summary> | ||
/// Suite of tests for the <see cref="ExceptionHandlerService"/> class. | ||
/// </summary> | ||
[TestFixture] | ||
public class ExceptionHandlerServiceTestFixture | ||
{ | ||
private Uri uri; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
this.uri = new Uri("http://www.stariongroup.eu"); | ||
} | ||
|
||
[Test] | ||
public void Test_if_basic_handling_works_and_returns_false() | ||
{ | ||
var testExceptionHandler = new TestExceptionHandler(false); | ||
var exceptionHandlerService = new ExceptionHandlerService(new[] { testExceptionHandler }); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(exceptionHandlerService.HandleException(new Exception()), Is.False); | ||
Assert.That(testExceptionHandler.WasExecuted, Is.True); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Test_if_basic_handling_works_and_returns_true() | ||
{ | ||
var testExceptionHandler = new TestExceptionHandler(true); | ||
var exceptionHandlerService = new ExceptionHandlerService(new[] { testExceptionHandler }); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(exceptionHandlerService.HandleException(new Exception()), Is.True); | ||
Assert.That(testExceptionHandler.WasExecuted, Is.True); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Test_that_multiple_handlers_return_true() | ||
{ | ||
var testExceptionHandler1 = new TestExceptionHandler(true); | ||
var testExceptionHandler2 = new TestExceptionHandler(true); | ||
var exceptionHandlerService = new ExceptionHandlerService(new[] { testExceptionHandler1, testExceptionHandler2 }); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(exceptionHandlerService.HandleException(new Exception()), Is.True); | ||
Assert.That(testExceptionHandler1.WasExecuted, Is.True); | ||
Assert.That(testExceptionHandler2.WasExecuted, Is.True); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Test_that_multiple_handlers_return_true_when_at_least_one_returns_true_1() | ||
{ | ||
var testExceptionHandler1 = new TestExceptionHandler(true); | ||
var testExceptionHandler2 = new TestExceptionHandler(false); | ||
var exceptionHandlerService = new ExceptionHandlerService(new[] { testExceptionHandler1, testExceptionHandler2 }); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(exceptionHandlerService.HandleException(new Exception()), Is.True); | ||
Assert.That(testExceptionHandler1.WasExecuted, Is.True); | ||
Assert.That(testExceptionHandler2.WasExecuted, Is.True); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Test_that_multiple_handlers_return_true_when_at_least_one_returns_true_2() | ||
{ | ||
var testExceptionHandler1 = new TestExceptionHandler(false); | ||
var testExceptionHandler2 = new TestExceptionHandler(true); | ||
var exceptionHandlerService = new ExceptionHandlerService(new[] { testExceptionHandler1, testExceptionHandler2 }); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(exceptionHandlerService.HandleException(new Exception()), Is.True); | ||
Assert.That(testExceptionHandler1.WasExecuted, Is.True); | ||
Assert.That(testExceptionHandler2.WasExecuted, Is.True); | ||
}); | ||
} | ||
} | ||
|
||
public class TestExceptionHandler : IExceptionHandler | ||
{ | ||
private bool result = false; | ||
public bool WasExecuted = false; | ||
|
||
public TestExceptionHandler(bool result) | ||
{ | ||
this.result = result; | ||
} | ||
|
||
public bool HandleException(Exception exception, params object[] payload) | ||
{ | ||
this.WasExecuted = true; | ||
return this.result; | ||
} | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
CDP4Common.Tests/ExceptionHandlerService/ExceptionHandlerServiceTestFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright file="ExceptionHandlerServiceTestFixture.cs" company="Starion Group S.A."> | ||
// Copyright (c) 2015-2024 Starion Group S.A. | ||
// | ||
// Author: Sam Gerené, Merlin Bieze, Alex Vorobiev, Naron Phou, Alexander van Delft | ||
// | ||
// This file is part of CDP4-COMET-SDK Community Edition | ||
// | ||
// The CDP4-COMET-SDK Community Edition is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 3 of the License, or (at your option) any later version. | ||
// | ||
// The CDP4-COMET-SDK Community Edition is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with this program; if not, write to the Free Software Foundation, | ||
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
namespace CDP4Common.Tests.Helpers | ||
{ | ||
using System; | ||
|
||
using NUnit.Framework; | ||
|
||
using ExceptionHandlerService; | ||
|
||
/// <summary> | ||
/// Suite of tests for the <see cref="ExceptionHandlerService"/> class. | ||
/// </summary> | ||
[TestFixture] | ||
public class ExceptionHandlerServiceTestFixture | ||
{ | ||
private Uri uri; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
this.uri = new Uri("http://www.stariongroup.eu"); | ||
} | ||
|
||
[Test] | ||
public void Test_if_basic_handling_works_and_returns_false() | ||
{ | ||
var testExceptionHandler = new TestExceptionHandler(false); | ||
var exceptionHandlerService = new ExceptionHandlerService(new[] { testExceptionHandler }); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(exceptionHandlerService.HandleException(new Exception()), Is.False); | ||
Assert.That(testExceptionHandler.WasExecuted, Is.True); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Test_if_basic_handling_works_and_returns_true() | ||
{ | ||
var testExceptionHandler = new TestExceptionHandler(true); | ||
var exceptionHandlerService = new ExceptionHandlerService(new[] { testExceptionHandler }); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(exceptionHandlerService.HandleException(new Exception()), Is.True); | ||
Assert.That(testExceptionHandler.WasExecuted, Is.True); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Test_that_multiple_handlers_return_true() | ||
{ | ||
var testExceptionHandler1 = new TestExceptionHandler(true); | ||
var testExceptionHandler2 = new TestExceptionHandler(true); | ||
var exceptionHandlerService = new ExceptionHandlerService(new[] { testExceptionHandler1, testExceptionHandler2 }); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(exceptionHandlerService.HandleException(new Exception()), Is.True); | ||
Assert.That(testExceptionHandler1.WasExecuted, Is.True); | ||
Assert.That(testExceptionHandler2.WasExecuted, Is.True); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Test_that_multiple_handlers_return_true_when_at_least_one_returns_true_1() | ||
{ | ||
var testExceptionHandler1 = new TestExceptionHandler(true); | ||
var testExceptionHandler2 = new TestExceptionHandler(false); | ||
var exceptionHandlerService = new ExceptionHandlerService(new[] { testExceptionHandler1, testExceptionHandler2 }); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(exceptionHandlerService.HandleException(new Exception()), Is.True); | ||
Assert.That(testExceptionHandler1.WasExecuted, Is.True); | ||
Assert.That(testExceptionHandler2.WasExecuted, Is.True); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Test_that_multiple_handlers_return_true_when_at_least_one_returns_true_2() | ||
{ | ||
var testExceptionHandler1 = new TestExceptionHandler(false); | ||
var testExceptionHandler2 = new TestExceptionHandler(true); | ||
var exceptionHandlerService = new ExceptionHandlerService(new[] { testExceptionHandler1, testExceptionHandler2 }); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(exceptionHandlerService.HandleException(new Exception()), Is.True); | ||
Assert.That(testExceptionHandler1.WasExecuted, Is.True); | ||
Assert.That(testExceptionHandler2.WasExecuted, Is.True); | ||
}); | ||
} | ||
} | ||
|
||
public class TestExceptionHandler : IExceptionHandler | ||
{ | ||
private bool result = false; | ||
public bool WasExecuted = false; | ||
|
||
public TestExceptionHandler(bool result) | ||
{ | ||
this.result = result; | ||
} | ||
|
||
public bool HandleException(Exception exception, params object[] payload) | ||
{ | ||
this.WasExecuted = true; | ||
return this.result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
CDP4Common/ExceptionHandlerService/ExceptionHandlerService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright file="ExceptionHandlerService.cs" company="Starion Group S.A."> | ||
// Copyright (c) 2015-2024 Starion Group S.A. | ||
// | ||
// Author: Sam Gerené, Alex Vorobiev, Alexander van Delft, Nathanael Smiechowski, Antoine Théate, Omar Elebiary | ||
// | ||
// This file is part of CDP4-COMET-IME Community Edition. | ||
// The CDP4-COMET-IME Community Edition is the Starion Concurrent Design Desktop Application and Excel Integration | ||
// compliant with ECSS-E-TM-10-25 Annex A and Annex C. | ||
// | ||
// The CDP4-COMET-IME Community Edition is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Affero General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 3 of the License, or any later version. | ||
// | ||
// The CDP4-COMET-IME Community Edition is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
namespace CDP4Common.ExceptionHandlerService | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
#if NETFRAMEWORK | ||
using System.ComponentModel.Composition; | ||
#endif | ||
using System.Linq; | ||
|
||
/// <summary> | ||
/// The purpose of the <see cref="ExceptionHandlerService" /> is to check server exceptions and start Application processes accordingly | ||
/// </summary> | ||
|
||
#if NETFRAMEWORK | ||
[Export(typeof(IExceptionHandlerService))] | ||
[PartCreationPolicy(CreationPolicy.Shared)] | ||
#endif | ||
public class ExceptionHandlerService : IExceptionHandlerService | ||
{ | ||
/// <summary> | ||
/// The collection of <see cref="IExceptionHandler"/>s | ||
/// </summary> | ||
private readonly List<IExceptionHandler> exceptionHandlers; | ||
|
||
/// <summary> | ||
/// Creates a new instance of the <see cref="ExceptionHandlerService"/> class | ||
/// </summary> | ||
/// <param name="exceptionHandlers">INJECTED collection of <see cref="IExceptionHandler"/>s registered in the application.</param> | ||
#if NETFRAMEWORK | ||
[ImportingConstructor] | ||
public ExceptionHandlerService([ImportMany] IEnumerable<IExceptionHandler> exceptionHandlers) | ||
{ | ||
this.exceptionHandlers = exceptionHandlers.ToList(); | ||
} | ||
#else | ||
public ExceptionHandlerService(IEnumerable<IExceptionHandler> exceptionHandlers) | ||
{ | ||
this.exceptionHandlers = exceptionHandlers.ToList(); | ||
} | ||
#endif | ||
|
||
/// <summary> | ||
/// Handles a specific <see cref="Exception"/> and enables the Application to start a process based on the ocntent or type of the <see cref="Exception"/> | ||
/// </summary> | ||
/// <param name="exception">The <see cref="Exception"/></param> | ||
/// <param name="payload"></param> | ||
/// <returns>a boolean value indicating if the <see cref="Exception"/> was handled or not, so it could be thrown again</returns> | ||
public bool HandleException(Exception exception, params object[] payload) | ||
{ | ||
var isHandled = false; | ||
|
||
foreach (var exceptionHandler in this.exceptionHandlers) | ||
{ | ||
isHandled = exceptionHandler.HandleException(exception, payload) || isHandled; | ||
} | ||
|
||
return isHandled; | ||
} | ||
} | ||
} |
Oops, something went wrong.