diff --git a/src/SAHB.GraphQLClient/Exceptions/GraphQLErrorException.cs b/src/SAHB.GraphQLClient/Exceptions/GraphQLErrorException.cs index d0c3b5d..9fa1d62 100644 --- a/src/SAHB.GraphQLClient/Exceptions/GraphQLErrorException.cs +++ b/src/SAHB.GraphQLClient/Exceptions/GraphQLErrorException.cs @@ -28,7 +28,17 @@ private static string GetMessage(IEnumerable errors) return string.Join(Environment.NewLine, errors.Select(error => - $"{error.Message ?? "Error"} at {Environment.NewLine}{string.Join(Environment.NewLine, error.Locations?.Select(location => " line: " + location.Line + " column: " + location.Column))}")); + $"{error.Message ?? "Error"} at {Environment.NewLine}{GetErrorLocation(error)}")); + } + + private static string GetErrorLocation(GraphQLDataError error) + { + var errors = (error.Locations?.Select(location => " line: " + location?.Line + " column: " + location?.Column)); + if (errors != null && errors.Any()) + { + return string.Join(Environment.NewLine, errors); + } + return "Unknown location"; } public IEnumerable Errors { get; } diff --git a/tests/SAHB.GraphQLClient.Tests/Exceptions/GraphQLErrorExceptionTests.cs b/tests/SAHB.GraphQLClient.Tests/Exceptions/GraphQLErrorExceptionTests.cs new file mode 100644 index 0000000..882db50 --- /dev/null +++ b/tests/SAHB.GraphQLClient.Tests/Exceptions/GraphQLErrorExceptionTests.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; +using SAHB.GraphQLClient.Exceptions; +using SAHB.GraphQLClient.Result; +using Xunit; + +namespace SAHB.GraphQLClient.Tests.Exceptions +{ + + public class GraphQLErrorExceptionTests + { + [Fact] + public void Get_Message_Should_Not_Throw_Exception() + { + var errorList = new List { new GraphQLDataError { Message = "Access denied." } }; + + new GraphQLErrorException("query", errorList); + } + } +}