Skip to content

Commit

Permalink
Exception when accesssing Data twice on MailChimpException (#362)
Browse files Browse the repository at this point in the history
* add MailchimpException test case

* prevent dictionary exception with accessing Data twice on MailChimpException
  • Loading branch information
eibx authored and brandonseydel committed Dec 6, 2018
1 parent eb38dc7 commit 1cec263
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
22 changes: 22 additions & 0 deletions MailChimp.Net.Tests/ExceptionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Net.Http;
using System.Threading.Tasks;
using MailChimp.Net.Core;
using MailChimp.Net.Models;
using Xunit;

namespace MailChimp.Net.Tests
{
public class ExceptionTest : MailChimpTest
{
[Fact]
public void Should_Return_Same_Data_On_Multiple_Calls()
{
var exception = new MailChimpException(new MailChimpApiError(), new HttpResponseMessage());

var data1 = exception.Data;
var data2 = exception.Data;

Assert.Equal(data1, data2);
}
}
}
9 changes: 8 additions & 1 deletion MailChimp.Net/Core/MailChimpException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ private static string formatMessage(MailChimpApiError apierror)
return builder.ToString();
}

private IDictionary _data;

public List<MailChimpError> Errors { get; set; }

/// <summary>
Expand Down Expand Up @@ -78,6 +80,9 @@ public override IDictionary Data
{
get
{
if (_data != null)
return _data;

var data = base.Data;
data.Add("detail", Detail);
data.Add("title", Title);
Expand All @@ -86,7 +91,9 @@ public override IDictionary Data
data.Add("instance", Instance);
data.Add("errors", Errors);
data.Add("rawhttpresponsemessage", RawHttpResponseMessage);
return data;

_data = data;
return _data;
}
}
}
Expand Down

0 comments on commit 1cec263

Please sign in to comment.