Deserialize errorType from extensions

The errorType field on GraphQLError is specific to GraphQL Java and when
serialized via GraphqlErrorHelper#toSpecification it becomes an
extension with the key "classification".

This commit ensures that we correctly deserialize back to errorType from
the "classification" extension but unfortunately that's limited to the
ErrorClassification enums that we know of.
This commit is contained in:
Rossen Stoyanchev
2021-06-29 14:52:33 +01:00
parent 71ce400e4f
commit bbf152a32f
3 changed files with 24 additions and 21 deletions

View File

@@ -66,8 +66,7 @@ class SampleApplicationTests {
.errors()
.satisfy(errors -> {
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getExtensions().get("classification"))
.isEqualTo(ErrorType.UNAUTHORIZED.name());
assertThat(errors.get(0).getErrorType()).isEqualTo(ErrorType.UNAUTHORIZED);
});
}
@@ -86,8 +85,7 @@ class SampleApplicationTests {
.errors()
.satisfy(errors -> {
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getExtensions().get("classification"))
.isEqualTo(ErrorType.FORBIDDEN.name());
assertThat(errors.get(0).getErrorType()).isEqualTo(ErrorType.FORBIDDEN);
});
}
@@ -118,8 +116,7 @@ class SampleApplicationTests {
.errors()
.satisfy(errors -> {
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getExtensions().get("classification"))
.isEqualTo(ErrorType.UNAUTHORIZED.name());
assertThat(errors.get(0).getErrorType()).isEqualTo(ErrorType.UNAUTHORIZED);
});
}

View File

@@ -51,8 +51,7 @@ class SampleApplicationTests {
.errors()
.satisfy(errors -> {
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getExtensions().get("classification"))
.isEqualTo(ErrorType.UNAUTHORIZED.name());
assertThat(errors.get(0).getErrorType()).isEqualTo(ErrorType.UNAUTHORIZED);
});
}
@@ -71,8 +70,7 @@ class SampleApplicationTests {
.errors()
.satisfy(errors -> {
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getExtensions().get("classification"))
.isEqualTo(ErrorType.FORBIDDEN.name());
assertThat(errors.get(0).getErrorType()).isEqualTo(ErrorType.FORBIDDEN);
});
}
@@ -103,8 +101,7 @@ class SampleApplicationTests {
.errors()
.satisfy(errors -> {
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getExtensions().get("classification"))
.isEqualTo(ErrorType.UNAUTHORIZED.name());
assertThat(errors.get(0).getErrorType()).isEqualTo(ErrorType.UNAUTHORIZED);
});
}

View File

@@ -42,9 +42,6 @@ class TestGraphQlError implements GraphQLError {
@Nullable
private List<SourceLocation> locations;
@Nullable
private ErrorClassification errorType;
@Nullable
private List<Object> path;
@@ -75,15 +72,27 @@ class TestGraphQlError implements GraphQLError {
return this.locations;
}
@SuppressWarnings("unused")
void setErrorType(ErrorClassification errorType) {
this.errorType = errorType;
}
@Override
@Nullable
public ErrorClassification getErrorType() {
return this.errorType;
// Attempt the reverse of how errorType is serialized in GraphqlErrorHelper.toSpecification.
// However we can only do that for ErrorClassification enums that we know of.
String value = (getExtensions() != null ? (String) getExtensions().get("classification") : null);
if (value != null) {
try {
return graphql.ErrorType.valueOf(value);
}
catch (IllegalArgumentException ex) {
// ignore
}
try {
return org.springframework.graphql.execution.ErrorType.valueOf(value);
}
catch (IllegalArgumentException ex) {
// ignore
}
}
return null;
}
@SuppressWarnings("unused")