From f3204bd72da29eab4348e6ad06587e6e629426c3 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Fri, 30 Nov 2018 12:35:58 +0100 Subject: [PATCH] Check aggregate exception during startup --- .../Exceptions/WireMockException.cs | 35 +++++++++++++++++++ src/WireMock.Net/Server/FluentMockServer.cs | 15 +++++--- 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 src/WireMock.Net/Exceptions/WireMockException.cs diff --git a/src/WireMock.Net/Exceptions/WireMockException.cs b/src/WireMock.Net/Exceptions/WireMockException.cs new file mode 100644 index 000000000..4e1e29bd1 --- /dev/null +++ b/src/WireMock.Net/Exceptions/WireMockException.cs @@ -0,0 +1,35 @@ +using System; + +namespace WireMock.Exceptions +{ + /// + /// WireMockException + /// + /// + public class WireMockException : Exception + { + /// + /// Initializes a new instance of the class. + /// + public WireMockException() + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The message that describes the error. + public WireMockException(string message) : base(message) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The message. + /// The inner. + public WireMockException(string message, Exception inner) : base(message, inner) + { + } + } +} \ No newline at end of file diff --git a/src/WireMock.Net/Server/FluentMockServer.cs b/src/WireMock.Net/Server/FluentMockServer.cs index 6ef7421a6..f9ec203ad 100644 --- a/src/WireMock.Net/Server/FluentMockServer.cs +++ b/src/WireMock.Net/Server/FluentMockServer.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading; using Newtonsoft.Json; +using WireMock.Exceptions; using WireMock.Handlers; using WireMock.Logging; using WireMock.Matchers; @@ -213,21 +214,27 @@ private FluentMockServer(IFluentMockServerSettings settings) #endif Ports = _httpServer.Ports; - _httpServer.StartAsync(); + var startTask = _httpServer.StartAsync(); using (var ctsStartTimeout = new CancellationTokenSource(settings.StartTimeout)) { while (!_httpServer.IsStarted) { - // Throw out exception if service start fails + // Throw exception if service start fails if (_httpServer.RunningException != null) { - throw new Exception($"Service start failed with error: {_httpServer.RunningException.Message}", _httpServer.RunningException); + throw new WireMockException($"Service start failed with error: {_httpServer.RunningException.Message}", _httpServer.RunningException); } - // Respect start timeout setting by throwing TimeoutException if (ctsStartTimeout.IsCancellationRequested) { + // In case of an aggregate exception, throw the exception. + if (startTask.Exception != null) + { + throw new WireMockException($"Service start failed with error: {startTask.Exception.Message}", startTask.Exception); + } + + // Else throw TimeoutException throw new TimeoutException($"Service start timed out after {TimeSpan.FromMilliseconds(settings.StartTimeout)}"); }