Add FailureAnalyzer for InvalidSchemaLocationsException

This commit adds a new `FailureAnalyzer` for exceptions thrown when no
GraphQL schema file could be found.
This lists configured locations (with `"classpath:..."` notation) and
also resolved locations - this helps developers figure out how the
pattern are resolved and why schema files cannot be found.

See gh-29140
This commit is contained in:
Brian Clozel
2021-12-21 08:32:48 +01:00
parent de808834f5
commit 5cf0a4db94
3 changed files with 112 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.graphql;
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis;
/**
* An implementation of {@link AbstractFailureAnalyzer} to analyze failures caused by
* {@link InvalidSchemaLocationsException}.
*
* @author Brian Clozel
*/
class InvalidSchemaLocationsExceptionFailureAnalyzer extends AbstractFailureAnalyzer<InvalidSchemaLocationsException> {
@Override
protected FailureAnalysis analyze(Throwable rootFailure, InvalidSchemaLocationsException cause) {
String message = "Could not find any GraphQL schema file under configured locations.";
StringBuilder action = new StringBuilder(
"Check that the following locations contain schema files: " + System.lineSeparator());
for (InvalidSchemaLocationsException.SchemaLocation schemaLocation : cause.getSchemaLocations()) {
action.append(String.format("- '%s' (%s)" + System.lineSeparator(), schemaLocation.getUri(),
schemaLocation.getLocation()));
}
return new FailureAnalysis(message, action.toString(), cause);
}
}

View File

@@ -163,6 +163,7 @@ org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.autoconfigure.data.redis.RedisUrlSyntaxFailureAnalyzer,\
org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzer,\
org.springframework.boot.autoconfigure.flyway.FlywayMigrationScriptMissingFailureAnalyzer,\
org.springframework.boot.autoconfigure.graphql.InvalidSchemaLocationsExceptionFailureAnalyzer,\
org.springframework.boot.autoconfigure.jdbc.DataSourceBeanCreationFailureAnalyzer,\
org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer,\
org.springframework.boot.autoconfigure.jooq.NoDslContextBeanFailureAnalyzer,\

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.graphql;
import org.junit.jupiter.api.Test;
import org.springframework.boot.diagnostics.FailureAnalysis;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link InvalidSchemaLocationsExceptionFailureAnalyzer}
*
* @author Brian Clozel
*/
class InvalidSchemaLocationsExceptionFailureAnalyzerTests {
private final InvalidSchemaLocationsExceptionFailureAnalyzer analyzer = new InvalidSchemaLocationsExceptionFailureAnalyzer();
private final ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
private final String[] missingLocation = new String[] {
"classpath:org/springframework/boot/autoconfigure/graphql/missing/" };
private final String[] existingLocation = new String[] {
"classpath:org/springframework/boot/autoconfigure/graphql/" };
@Test
void shouldReportCause() {
InvalidSchemaLocationsException exception = new InvalidSchemaLocationsException(this.existingLocation,
this.resolver);
FailureAnalysis analysis = this.analyzer.analyze(exception);
assertThat(analysis.getCause()).isInstanceOf(InvalidSchemaLocationsException.class);
assertThat(analysis.getAction()).contains("Check that the following locations contain schema files:");
}
@Test
void shouldListUnresolvableLocation() {
InvalidSchemaLocationsException exception = new InvalidSchemaLocationsException(this.missingLocation,
this.resolver);
FailureAnalysis analysis = this.analyzer.analyze(exception);
assertThat(analysis.getAction()).contains(this.existingLocation[0]).doesNotContain("file:");
}
@Test
void shouldListExistingLocation() {
InvalidSchemaLocationsException exception = new InvalidSchemaLocationsException(this.existingLocation,
this.resolver);
FailureAnalysis analysis = this.analyzer.analyze(exception);
assertThat(analysis.getAction()).contains(this.existingLocation[0]).contains("file:");
}
}