From 5cf0a4db9427e7da1dedf8eb6f0a28a6282c2e98 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 21 Dec 2021 08:32:48 +0100 Subject: [PATCH] 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 --- ...hemaLocationsExceptionFailureAnalyzer.java | 42 +++++++++++ .../main/resources/META-INF/spring.factories | 1 + ...ocationsExceptionFailureAnalyzerTests.java | 69 +++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/InvalidSchemaLocationsExceptionFailureAnalyzer.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/InvalidSchemaLocationsExceptionFailureAnalyzerTests.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/InvalidSchemaLocationsExceptionFailureAnalyzer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/InvalidSchemaLocationsExceptionFailureAnalyzer.java new file mode 100644 index 0000000000..66b8aa34bf --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/InvalidSchemaLocationsExceptionFailureAnalyzer.java @@ -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 { + + @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); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 5931fe4350..71dda4d197 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -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,\ diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/InvalidSchemaLocationsExceptionFailureAnalyzerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/InvalidSchemaLocationsExceptionFailureAnalyzerTests.java new file mode 100644 index 0000000000..24bc06be73 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/InvalidSchemaLocationsExceptionFailureAnalyzerTests.java @@ -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:"); + } + +}