Skip to content

Commit

Permalink
Improve GraphQL errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lwih committed Dec 15, 2023
1 parent aefc60a commit 95d91e2
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ fun main(args: Array<String>) {
if (isSentryEnabled == "true") {
Sentry.init { options ->
options.dsn = sentryDsn
// Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
// We recommend adjusting this value in production.
options.tracesSampleRate = 1.0
}
}
Expand Down
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()
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -31,125 +31,125 @@ class GetEnvMissions(private val mapper: ObjectMapper) {
controlUnits: List<Int>? = null
): List<MissionEntity> {

try {
val uriBuilder = StringBuilder("https://monitorenv.din.developpement-durable.gouv.fr/api/v1/missions")

// Append query parameters
uriBuilder.append("?")

// Append optional parameters
startedAfterDateTime?.let {
uriBuilder.append(
"startedAfterDateTime=${
UriUtils.encodeQueryParam(
it.toString(),
"UTF-8"
)
}&"
)
}
startedBeforeDateTime?.let {
uriBuilder.append(
"startedBeforeDateTime=${
UriUtils.encodeQueryParam(
it.toString(),
"UTF-8"
)
}&"
)
}
pageNumber?.let { uriBuilder.append("pageNumber=${UriUtils.encodeQueryParam(it.toString(), "UTF-8")}&") }
pageSize?.let { uriBuilder.append("pageSize=${UriUtils.encodeQueryParam(it.toString(), "UTF-8")}&") }

// Append controlUnits if present
controlUnits?.let {
for (controlUnit in it) {
uriBuilder.append("controlUnits=${UriUtils.encodeQueryParam(controlUnit.toString(), "UTF-8")}&")
}
}

// Remove trailing "&" if present
if (uriBuilder.endsWith("&")) {
uriBuilder.deleteCharAt(uriBuilder.length - 1)
}

// Build the URI
val uri = URI.create(uriBuilder.toString())

val client: HttpClient = HttpClient.newBuilder().build()
val request = HttpRequest.newBuilder()
.uri(uri)
.build();

val response = client.send(request, HttpResponse.BodyHandlers.ofString())

mapper.registerModule(JtsModule())

val envMissions: List<EnvMission> =
mapper.readValue(response.body(), object : TypeReference<List<EnvMission>>() {})

logger.info("Found ${envMissions.size} missions ")

val missions = envMissions.map { MissionEntity(envMission = ExtendedEnvMissionEntity.fromEnvMission(it)) }

return missions
} catch (e: Exception) {
logger.error("GetEnvMissions failed loading Missions", e)
Sentry.captureMessage("GetEnvMissions failed loading Missions")
Sentry.captureException(e)

val mission1 = EnvMission(
id = 10,
missionTypes = listOf(MissionTypeEnum.SEA),
facade = "Outre-Mer",
observationsCacem = null,
startDateTimeUtc = ZonedDateTime.parse("2022-03-15T04:50:09Z"),
// endDateTimeUtc = ZonedDateTime.parse("2022-03-23T20:29:03Z"),
isClosed = false,
isDeleted = false,
missionSource = MissionSourceEnum.RAPPORTNAV,
hasMissionOrder = false,
isUnderJdp = false,
isGeometryComputedFromControls = false,
// try {
val uriBuilder = StringBuilder("https://monitorenv.din.developpement-durable.gouv.fr/api/v1/missions")

// Append query parameters
uriBuilder.append("?")

// Append optional parameters
startedAfterDateTime?.let {
uriBuilder.append(
"startedAfterDateTime=${
UriUtils.encodeQueryParam(
it.toString(),
"UTF-8"
)
}&"
)
val mission2 = EnvMission(
id = 11,
missionTypes = listOf(MissionTypeEnum.SEA),
facade = "Outre-Mer",
observationsCacem = null,
startDateTimeUtc = ZonedDateTime.parse("2022-02-15T04:50:09Z"),
endDateTimeUtc = ZonedDateTime.parse("2022-02-23T20:29:03Z"),
isClosed = true,
isDeleted = false,
missionSource = MissionSourceEnum.MONITORENV,
hasMissionOrder = false,
isUnderJdp = false,
isGeometryComputedFromControls = false
)
val mission3 = EnvMission(
id = 12,
missionTypes = listOf(MissionTypeEnum.SEA),
facade = "Outre-Mer",
observationsCacem = null,
startDateTimeUtc = ZonedDateTime.parse("2022-01-15T04:50:09Z"),
endDateTimeUtc = ZonedDateTime.parse("2022-01-23T20:29:03Z"),
isClosed = true,
isDeleted = false,
missionSource = MissionSourceEnum.MONITORFISH,
hasMissionOrder = false,
isUnderJdp = false,
isGeometryComputedFromControls = false
}
startedBeforeDateTime?.let {
uriBuilder.append(
"startedBeforeDateTime=${
UriUtils.encodeQueryParam(
it.toString(),
"UTF-8"
)
}&"
)
}
pageNumber?.let { uriBuilder.append("pageNumber=${UriUtils.encodeQueryParam(it.toString(), "UTF-8")}&") }
pageSize?.let { uriBuilder.append("pageSize=${UriUtils.encodeQueryParam(it.toString(), "UTF-8")}&") }

val envMissions = listOf(mission1, mission2, mission3)

val missions = envMissions.map { MissionEntity(envMission = ExtendedEnvMissionEntity.fromEnvMission(it)) }

logger.info("Found ${missions.size} missions ")
// Append controlUnits if present
controlUnits?.let {
for (controlUnit in it) {
uriBuilder.append("controlUnits=${UriUtils.encodeQueryParam(controlUnit.toString(), "UTF-8")}&")
}
}

return missions
// Remove trailing "&" if present
if (uriBuilder.endsWith("&")) {
uriBuilder.deleteCharAt(uriBuilder.length - 1)
}

// Build the URI
val uri = URI.create(uriBuilder.toString())

val client: HttpClient = HttpClient.newBuilder().build()
val request = HttpRequest.newBuilder()
.uri(uri)
.build();

val response = client.send(request, HttpResponse.BodyHandlers.ofString())

mapper.registerModule(JtsModule())

val envMissions: List<EnvMission> =
mapper.readValue(response.body(), object : TypeReference<List<EnvMission>>() {})

logger.info("Found ${envMissions.size} missions ")

val missions = envMissions.map { MissionEntity(envMission = ExtendedEnvMissionEntity.fromEnvMission(it)) }

return missions
// } catch (e: Exception) {
// logger.error("GetEnvMissions failed loading Missions", e)
// Sentry.captureMessage("GetEnvMissions failed loading Missions")
// Sentry.captureException(e)
//
// val mission1 = EnvMission(
// id = 10,
// missionTypes = listOf(MissionTypeEnum.SEA),
// facade = "Outre-Mer",
// observationsCacem = null,
// startDateTimeUtc = ZonedDateTime.parse("2022-03-15T04:50:09Z"),
//// endDateTimeUtc = ZonedDateTime.parse("2022-03-23T20:29:03Z"),
// isClosed = false,
// isDeleted = false,
// missionSource = MissionSourceEnum.RAPPORTNAV,
// hasMissionOrder = false,
// isUnderJdp = false,
// isGeometryComputedFromControls = false,
// )
// val mission2 = EnvMission(
// id = 11,
// missionTypes = listOf(MissionTypeEnum.SEA),
// facade = "Outre-Mer",
// observationsCacem = null,
// startDateTimeUtc = ZonedDateTime.parse("2022-02-15T04:50:09Z"),
// endDateTimeUtc = ZonedDateTime.parse("2022-02-23T20:29:03Z"),
// isClosed = true,
// isDeleted = false,
// missionSource = MissionSourceEnum.MONITORENV,
// hasMissionOrder = false,
// isUnderJdp = false,
// isGeometryComputedFromControls = false
// )
// val mission3 = EnvMission(
// id = 12,
// missionTypes = listOf(MissionTypeEnum.SEA),
// facade = "Outre-Mer",
// observationsCacem = null,
// startDateTimeUtc = ZonedDateTime.parse("2022-01-15T04:50:09Z"),
// endDateTimeUtc = ZonedDateTime.parse("2022-01-23T20:29:03Z"),
// isClosed = true,
// isDeleted = false,
// missionSource = MissionSourceEnum.MONITORFISH,
// hasMissionOrder = false,
// isUnderJdp = false,
// isGeometryComputedFromControls = false
// )
//
// val envMissions = listOf(mission1, mission2, mission3)
//
// val missions = envMissions.map { MissionEntity(envMission = ExtendedEnvMissionEntity.fromEnvMission(it)) }
//
// logger.info("Found ${missions.size} missions ")
//
// return missions
// }


}
}

0 comments on commit 95d91e2

Please sign in to comment.