diff --git a/PinSharp.Tests/Api/Exceptions/PinSharpExceptionsTest.cs b/PinSharp.Tests/Api/Exceptions/PinSharpExceptionsTest.cs new file mode 100644 index 0000000..bdcda8a --- /dev/null +++ b/PinSharp.Tests/Api/Exceptions/PinSharpExceptionsTest.cs @@ -0,0 +1,39 @@ +using FluentAssertions; +using PinSharp.Api.Exceptions; +using Xunit; + +namespace PinSharp.Tests.Api.Exceptions +{ + public class PinSharpExceptionsTest + { + [Fact] + public void Create_HttpStatusCode_ShouldBeSetToPassedValue() + { + var expectedStatusCode = 500; + + var exception = PinSharpException.Create(null, null, null, expectedStatusCode); + + exception.HttpStatusCode.Should().Be(expectedStatusCode); + } + + [Fact] + public void Create_HttpStatusCode_NullValueShouldNotOverwriteExceptionValue() + { + var expectedStatusCode = MockException.DefaultStatusCode; + + var exception = PinSharpException.Create(null, null, null, null); + + exception.HttpStatusCode.Should().Be(expectedStatusCode); + } + + public class MockException : PinSharpException + { + public const int DefaultStatusCode = 400; + + public MockException(string message) : base(message) + { + HttpStatusCode = DefaultStatusCode; + } + } + } +} diff --git a/PinSharp/Api/Exceptions/PinSharpException.cs b/PinSharp/Api/Exceptions/PinSharpException.cs index 8a37ed6..3ab8f0f 100644 --- a/PinSharp/Api/Exceptions/PinSharpException.cs +++ b/PinSharp/Api/Exceptions/PinSharpException.cs @@ -33,7 +33,10 @@ internal static T Create(string message, string requestUrl, string responseCo var exception = (T) Activator.CreateInstance(typeof (T), message); exception.RequestUrl = requestUrl; exception.ResponseContent = responseContent; - exception.HttpStatusCode = httpStatusCode; + + if (httpStatusCode != null) + exception.HttpStatusCode = httpStatusCode; + return exception; } }