-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
151 additions
and
115 deletions.
There are no files selected for viewing
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
38 changes: 38 additions & 0 deletions
38
backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/config/GraphQLExceptionHandler.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,38 @@ | ||
package fr.gouv.dgampa.rapportnav.config | ||
|
||
import graphql.ErrorType | ||
import graphql.GraphQLError | ||
import graphql.GraphqlErrorBuilder | ||
import graphql.schema.DataFetchingEnvironment | ||
import org.hibernate.exception.ConstraintViolationException | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.data.crossstore.ChangeSetPersister | ||
import org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class GraphQLExceptionHandler : DataFetcherExceptionResolverAdapter() { | ||
companion object { | ||
private val log = LoggerFactory.getLogger(this::class.java) | ||
} | ||
|
||
override fun resolveToSingleError(e: Throwable, env: DataFetchingEnvironment): GraphQLError? { | ||
return when (e) { | ||
// TODO add more cases | ||
is Exception -> toGraphQLError(e, env) | ||
else -> super.resolveToSingleError(e, env) | ||
} | ||
} | ||
|
||
private fun toGraphQLError(e: Throwable, env: DataFetchingEnvironment): GraphQLError? { | ||
log.warn("Exception while handling request: ${e.message}", e) | ||
return GraphqlErrorBuilder.newError() | ||
.message(e.message) | ||
.errorType(ErrorType.DataFetchingException) | ||
.path(env.executionStepInfo.path) | ||
.location(env.field.sourceLocation) | ||
.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