Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 02d8319

Browse files
feat: improve to string method of GraphQLTestError; add tests for it
1 parent 4412cad commit 02d8319

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestError.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import lombok.Builder;
1010
import lombok.Data;
1111
import lombok.NoArgsConstructor;
12+
import org.springframework.util.NumberUtils;
1213

1314
import java.util.List;
1415
import java.util.Map;
1516
import java.util.Optional;
17+
import java.util.stream.Collectors;
1618

1719
import static java.util.Objects.nonNull;
1820

@@ -35,17 +37,36 @@ public class GraphQLTestError implements GraphQLError {
3537
@Override
3638
public String toString() {
3739
final StringBuilder sb = new StringBuilder();
38-
sb.append(Optional.of(message).orElse("Error without error message"));
40+
sb.append(Optional.ofNullable(errorType).map(ErrorClassification::toString).orElse("<Unspecified error>"));
41+
sb.append(": ");
42+
sb.append(Optional.ofNullable(message).orElse("<error message not provided>"));
3943
if (nonNull(locations) && !locations.isEmpty()) {
4044
sb.append(" at line ");
4145
locations.forEach(
4246
location -> sb
4347
.append(location.getLine())
4448
.append(", column ")
4549
.append(location.getColumn()).append(" in ")
46-
.append(Optional.ofNullable(location.getSourceName()).orElse("unnamed/unspecified source."))
50+
.append(Optional.ofNullable(location.getSourceName()).orElse("unnamed/unspecified source"))
4751
);
4852
}
53+
if (nonNull(path) && !path.isEmpty()) {
54+
sb.append(". Selection path: ");
55+
sb.append(path.stream()
56+
.map(Object::toString)
57+
.map(this::toNumericIndexIfPossible)
58+
.collect(Collectors.joining("/"))
59+
.replaceAll("/\\[", "[")
60+
);
61+
}
4962
return sb.toString();
5063
}
64+
65+
private String toNumericIndexIfPossible(final String s) {
66+
try {
67+
return "[" + NumberUtils.parseNumber(s, Long.class) + "]";
68+
} catch (IllegalArgumentException e) {
69+
return s;
70+
}
71+
}
5172
}

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLResponseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void testExpectNoErrorsFail() {
186186
// THEN
187187
assertThatExceptionOfType(AssertionError.class)
188188
.isThrownBy(graphQLResponse::assertThatNoErrorsArePresent)
189-
.withMessage("Expected no GraphQL errors, but got 1: Test error.");
189+
.withMessage("Expected no GraphQL errors, but got 1: <Unspecified error>: Test error.");
190190
}
191191

192192
@Test
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.graphql.spring.boot.test;
2+
3+
import graphql.ErrorType;
4+
import graphql.GraphQLError;
5+
import graphql.language.SourceLocation;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
import java.util.Arrays;
11+
import java.util.Collections;
12+
import java.util.stream.Stream;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
16+
public class GraphQLTestErrorToStringTest {
17+
18+
private static Stream<Arguments> toStringTestArguments() {
19+
return Stream.of(
20+
Arguments.of(
21+
new GraphQLTestError(null, null, null, null, null),
22+
"<Unspecified error>: <error message not provided>"
23+
),
24+
Arguments.of(
25+
new GraphQLTestError("Test message", null, null, null, null),
26+
"<Unspecified error>: Test message"
27+
),
28+
Arguments.of(
29+
new GraphQLTestError("Test message", null, ErrorType.DataFetchingException, null, null),
30+
"DataFetchingException: Test message"
31+
),
32+
Arguments.of(
33+
new GraphQLTestError("Test message", Collections.emptyList(), ErrorType.DataFetchingException, null, null),
34+
"DataFetchingException: Test message"
35+
),
36+
Arguments.of(
37+
new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2)),
38+
ErrorType.DataFetchingException, null, null),
39+
"DataFetchingException: Test message at line 1, column 2 in unnamed/unspecified source"
40+
),
41+
Arguments.of(
42+
new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2)),
43+
ErrorType.DataFetchingException, Collections.emptyList(), null),
44+
"DataFetchingException: Test message at line 1, column 2 in unnamed/unspecified source"
45+
),
46+
Arguments.of(
47+
new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2)),
48+
ErrorType.DataFetchingException, Arrays.asList("path", "to", "error"), null),
49+
"DataFetchingException: Test message at line 1, column 2 in unnamed/unspecified source. Selection path: path/to/error"
50+
),
51+
Arguments.of(
52+
new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2, "test.graphql")),
53+
ErrorType.DataFetchingException, Arrays.asList("path", "to", "error"), null),
54+
"DataFetchingException: Test message at line 1, column 2 in test.graphql. Selection path: path/to/error"
55+
),
56+
Arguments.of(
57+
new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2)),
58+
ErrorType.DataFetchingException, Arrays.asList("path", 123, "error"), null),
59+
"DataFetchingException: Test message at line 1, column 2 in unnamed/unspecified source. Selection path: path[123]/error"
60+
),
61+
Arguments.of(
62+
new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2)),
63+
ErrorType.DataFetchingException, Arrays.asList("path", 123, "error"), Collections.singletonMap("please ignore", "this")),
64+
"DataFetchingException: Test message at line 1, column 2 in unnamed/unspecified source. Selection path: path[123]/error"
65+
)
66+
);
67+
}
68+
69+
@ParameterizedTest(name = "{1}")
70+
@MethodSource("toStringTestArguments")
71+
void testGraphQLTestErrorToString(final GraphQLError error, final String expectedToString) {
72+
assertThat(error.toString()).isEqualTo(expectedToString);
73+
}
74+
}

0 commit comments

Comments
 (0)