Skip to content

Commit

Permalink
Fix HttpStatusCode of exceptions being overwritten
Browse files Browse the repository at this point in the history
  • Loading branch information
Krusen committed Oct 31, 2019
1 parent 7bbb264 commit 6cc29cf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
39 changes: 39 additions & 0 deletions PinSharp.Tests/Api/Exceptions/PinSharpExceptionsTest.cs
Original file line number Diff line number Diff line change
@@ -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<MockException>(null, null, null, expectedStatusCode);

exception.HttpStatusCode.Should().Be(expectedStatusCode);
}

[Fact]
public void Create_HttpStatusCode_NullValueShouldNotOverwriteExceptionValue()
{
var expectedStatusCode = MockException.DefaultStatusCode;

var exception = PinSharpException.Create<MockException>(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;
}
}
}
}
5 changes: 4 additions & 1 deletion PinSharp/Api/Exceptions/PinSharpException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ internal static T Create<T>(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;
}
}
Expand Down

0 comments on commit 6cc29cf

Please sign in to comment.