-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2040 from Netflix/fix/fix-controller-advice
Fix setup of exception handling to make @ControllerAdvice work
- Loading branch information
Showing
6 changed files
with
160 additions
and
13 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...src/main/java/com/netflix/graphql/dgs/example/datafetcher/ControllerExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2024 Netflix, Inc. | ||
* | ||
* 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 | ||
* | ||
* http://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 com.netflix.graphql.dgs.example.datafetcher; | ||
|
||
import graphql.GraphQLError; | ||
import org.springframework.graphql.data.method.annotation.GraphQlExceptionHandler; | ||
import org.springframework.graphql.execution.ErrorType; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
|
||
@ControllerAdvice | ||
public class ControllerExceptionHandler { | ||
@GraphQlExceptionHandler | ||
public GraphQLError handle(IllegalArgumentException ex) { | ||
return GraphQLError.newError().errorType(ErrorType.BAD_REQUEST).message("Successful error handling").build(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...-java/src/main/java/com/netflix/graphql/dgs/example/datafetcher/WithControllerAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2024 Netflix, Inc. | ||
* | ||
* 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 | ||
* | ||
* http://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 com.netflix.graphql.dgs.example.datafetcher; | ||
|
||
import com.netflix.graphql.dgs.DgsComponent; | ||
import com.netflix.graphql.dgs.DgsQuery; | ||
|
||
@DgsComponent | ||
public class WithControllerAdvice { | ||
@DgsQuery | ||
public String withControllerAdvice() { | ||
throw new IllegalArgumentException("Testing controller advice"); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
graphql-dgs-spring-graphql-example-java/src/main/resources/schema/schema.graphqls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
.../src/test/kotlin/com/netflix/graphql/dgs/springgraphql/autoconfig/ControllerAdviceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2024 Netflix, Inc. | ||
* | ||
* 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 | ||
* | ||
* http://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 com.netflix.graphql.dgs.springgraphql.autoconfig | ||
|
||
import com.netflix.graphql.dgs.DgsComponent | ||
import com.netflix.graphql.dgs.DgsQuery | ||
import com.netflix.graphql.dgs.DgsQueryExecutor | ||
import com.netflix.graphql.dgs.autoconfig.DgsAutoConfiguration | ||
import graphql.GraphQLError | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.intellij.lang.annotations.Language | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.autoconfigure.graphql.GraphQlAutoConfiguration | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.boot.test.context.TestConfiguration | ||
import org.springframework.graphql.data.method.annotation.GraphQlExceptionHandler | ||
import org.springframework.graphql.execution.ErrorType | ||
import org.springframework.web.bind.annotation.ControllerAdvice | ||
|
||
@SpringBootTest( | ||
classes = [ | ||
DgsSpringGraphQLAutoConfiguration::class, | ||
DgsAutoConfiguration::class, | ||
DgsSpringGraphQLSourceAutoConfiguration::class, | ||
GraphQlAutoConfiguration::class, | ||
ControllerAdviceTest.ControllerAdviceTestConfig::class, | ||
], | ||
properties = [ | ||
"dgs.graphql.schema-locations=classpath:/dgs-spring-graphql-smoke-test.graphqls", | ||
"spring.graphql.schema.inspection.enabled=true", | ||
"dgs.graphql.schema-wiring-validation-enabled=false", | ||
], | ||
) | ||
class ControllerAdviceTest { | ||
@Autowired | ||
lateinit var queryExecutor: DgsQueryExecutor | ||
|
||
@Test | ||
fun testControllerAdvice() { | ||
@Language("GraphQL") | ||
val query = | ||
""" | ||
query { | ||
withControllerAdvice | ||
} | ||
""".trimIndent() | ||
|
||
val result = queryExecutor.execute(query) | ||
assertThat(result.errors).isNotEmpty | ||
assertThat(result.errors.first().message).isEqualTo("Successful error handling") | ||
assertThat(result.errors.first().errorType).isEqualTo(ErrorType.BAD_REQUEST) | ||
} | ||
|
||
@TestConfiguration | ||
open class ControllerAdviceTestConfig { | ||
@DgsComponent | ||
class TestDataFetcher { | ||
@DgsQuery | ||
fun withControllerAdvice(): Unit = throw IllegalArgumentException("Testing Controller Advice") | ||
} | ||
|
||
@ControllerAdvice | ||
class TestControllerAdvice { | ||
@GraphQlExceptionHandler | ||
fun handle(ex: java.lang.IllegalArgumentException?): GraphQLError = | ||
GraphQLError | ||
.newError() | ||
.errorType(ErrorType.BAD_REQUEST) | ||
.message("Successful error handling") | ||
.build() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters