Skip to content

Commit

Permalink
[ADD] Implement exception handler service; fixes #345
Browse files Browse the repository at this point in the history
* 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
lxatstariongroup authored Jul 2, 2024
1 parent 13c0753 commit 1e4ff16
Show file tree
Hide file tree
Showing 24 changed files with 609 additions and 45 deletions.
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;
}
}
}
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;
}
}
}
4 changes: 2 additions & 2 deletions CDP4Common/CDP4Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net48;netstandard2.0</TargetFrameworks>
<Company>Starion Group S.A.</Company>
<Title>CDP4Common Community Edition</Title>
<VersionPrefix>27.0.2</VersionPrefix>
<VersionPrefix>27.1.0</VersionPrefix>
<Description>CDP4 Common Class Library that contains DTOs, POCOs</Description>
<Copyright>Copyright © Starion Group S.A.</Copyright>
<Authors>Sam, Merlin, Alex, Naron, Alexander, Yevhen, Nathanael, Ahmed</Authors>
Expand All @@ -20,7 +20,7 @@
<PackageTags>CDP COMET ECSS-E-TM-10-25</PackageTags>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageReleaseNotes>
[ADD] Extension method for human readable texts on IRuleVerification
[ADD] Implement ExceptionHandlerService
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
Expand Down
85 changes: 85 additions & 0 deletions CDP4Common/ExceptionHandlerService/ExceptionHandlerService.cs
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;
}
}
}
Loading

0 comments on commit 1e4ff16

Please sign in to comment.