Skip to content

Commit

Permalink
Improved GraphQLClientException message when there is only one GraphQ…
Browse files Browse the repository at this point in the history
…L error. (ChilliCream#4528)
  • Loading branch information
jorrit authored Dec 9, 2021
1 parent 48d071e commit 0a82323
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/StrawberryShake/Client/src/Core/GraphQLClientException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,28 @@ public GraphQLClientException(params IClientError[] errors)
throw new ArgumentNullException(nameof(errors));
}

var message = new StringBuilder("Multiple errors occured:");

foreach (var error in errors)
if (errors.Length == 0)
{
Message = "Unknown error occurred";
}
else if (errors.Length == 1)
{
message.Append("- ");
message.AppendLine(error.Message);
Message = errors[0].Message;
}
else
{
var message = new StringBuilder("Multiple errors occurred:");

foreach (IClientError error in errors)
{
message.AppendLine();
message.Append("- ");
message.Append(error.Message);
}

Message = message.ToString();
}

Message = message.ToString();
Errors = errors;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Xunit;

namespace StrawberryShake
{
public class GraphQLClientExceptionTests
{
[Fact]
public void Constructor_ZeroErrors()
{
//arrange
var errors = new IClientError[0];

//act
var exception = new GraphQLClientException(errors);

//assert
Assert.Equal("Unknown error occurred", exception.Message);
}

[Fact]
public void Constructor_OneError()
{
//arrange
var errors = new IClientError[]
{
new ClientError("some message"),
};

//act
var exception = new GraphQLClientException(errors);

//assert
Assert.Equal("some message", exception.Message);
}

[Fact]
public void Constructor_TwoErrors()
{
//arrange
var errors = new IClientError[]
{
new ClientError("first message"),
new ClientError("second message"),
};

//act
var exception = new GraphQLClientException(errors);

//assert
Assert.Equal(@"Multiple errors occurred:
- first message
- second message", exception.Message.Replace("\r", ""));
}
}
}

0 comments on commit 0a82323

Please sign in to comment.