Skip to content

Commit

Permalink
Rename the error type enum
Browse files Browse the repository at this point in the history
Since this doesn't have anything to do with Hasura itself,
other than that it is included in responses that go through Hasura.
  • Loading branch information
Leitsi committed Dec 14, 2023
1 parent ec6f760 commit b11ca33
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,76 +18,76 @@ sealed interface HasuraErrorExtensions {
sealed interface JoreErrorExtensions : HasuraErrorExtensions {
// Not required by Hasura, but we always want to provide one
// so that the UI can present nice detailed error messages.
val type: HasuraErrorType
val type: TimetablesApiErrorType
}

data class PlainStatusExtensions(
override val code: Int,
override val type: HasuraErrorType
override val type: TimetablesApiErrorType
) : JoreErrorExtensions {

constructor(httpStatus: HttpStatus) : this(
httpStatus.value(),
HasuraErrorType.UnknownError
TimetablesApiErrorType.UnknownError
)
}

data class InvalidTargetPriorityExtensions(
override val code: Int,
override val type: HasuraErrorType,
override val type: TimetablesApiErrorType,
val targetPriority: TimetablesPriority
) : JoreErrorExtensions {

companion object {
fun from(ex: InvalidTargetPriorityException) = InvalidTargetPriorityExtensions(
HttpStatus.BAD_REQUEST.value(),
HasuraErrorType.InvalidTargetPriorityError,
TimetablesApiErrorType.InvalidTargetPriorityError,
ex.targetPriority
)
}
}

data class StagingVehicleScheduleFrameNotFoundExtensions(
override val code: Int,
override val type: HasuraErrorType,
override val type: TimetablesApiErrorType,
val stagingVehicleScheduleFrameId: UUID
) : JoreErrorExtensions {

companion object {
fun from(ex: StagingVehicleScheduleFrameNotFoundException) = StagingVehicleScheduleFrameNotFoundExtensions(
HttpStatus.NOT_FOUND.value(),
HasuraErrorType.StagingVehicleScheduleFrameNotFoundError,
TimetablesApiErrorType.StagingVehicleScheduleFrameNotFoundError,
ex.stagingVehicleScheduleFrameId
)
}
}

data class TargetVehicleScheduleFrameNotFoundExtensions(
override val code: Int,
override val type: HasuraErrorType,
override val type: TimetablesApiErrorType,
val stagingVehicleScheduleFrameId: UUID
) : JoreErrorExtensions {

companion object {
fun from(ex: TargetFrameNotFoundException) = TargetVehicleScheduleFrameNotFoundExtensions(
HttpStatus.NOT_FOUND.value(),
HasuraErrorType.TargetVehicleScheduleFrameNotFoundError,
TimetablesApiErrorType.TargetVehicleScheduleFrameNotFoundError,
ex.stagingVehicleScheduleFrameId
)
}
}

data class MultipleTargetFramesFoundExtensions(
override val code: Int,
override val type: HasuraErrorType,
override val type: TimetablesApiErrorType,
val stagingVehicleScheduleFrameId: UUID,
val targetVehicleScheduleFrameIds: List<UUID>
) : JoreErrorExtensions {

companion object {
fun from(ex: MultipleTargetFramesFoundException) = MultipleTargetFramesFoundExtensions(
HttpStatus.CONFLICT.value(),
HasuraErrorType.MultipleTargetFramesFoundError,
TimetablesApiErrorType.MultipleTargetFramesFoundError,
ex.stagingVehicleScheduleFrameId,
ex.targetVehicleScheduleFrameIds
)
Expand All @@ -96,49 +96,49 @@ data class MultipleTargetFramesFoundExtensions(

data class TargetPriorityParsingExtensions(
override val code: Int,
override val type: HasuraErrorType,
override val type: TimetablesApiErrorType,
val targetPriority: Int
) : JoreErrorExtensions {

companion object {
fun from(ex: TargetPriorityParsingException) = TargetPriorityParsingExtensions(
HttpStatus.BAD_REQUEST.value(),
HasuraErrorType.TargetPriorityParsingError,
TimetablesApiErrorType.TargetPriorityParsingError,
ex.targetPriority
)
}
}

data class TransactionSystemExtensions(
override val code: Int,
override val type: HasuraErrorType,
override val type: TimetablesApiErrorType,
val sqlErrorMessage: String
) : JoreErrorExtensions {

companion object {
fun from(ex: TransactionSystemException) = TransactionSystemExtensions(
HttpStatus.CONFLICT.value(),
resolveHasuraErrorType(ex),
resolveErrorType(ex),
ex.cause?.message ?: "unknown error on transaction commit or rollback"
)

private fun resolveHasuraErrorType(ex: TransactionSystemException): HasuraErrorType {
private fun resolveErrorType(ex: TransactionSystemException): TimetablesApiErrorType {
val errorMessage = ex.cause?.message ?: "" // Should always be defined though.

// Attempt to detect from the error message (because there's no other data really) which error case triggered the failure.
// Mainly using either the triggering validation function name or some known part of the error message for this.
return errorTypesWithMatchingSubstrings.find { it.second in errorMessage }?.first
?: HasuraErrorType.TransactionSystemError
?: TimetablesApiErrorType.TransactionSystemError
}

private val errorTypesWithMatchingSubstrings = listOf(
HasuraErrorType.PassingTimeStopPointMatchingOrderError to "passing times and their matching stop points must be in same order",
HasuraErrorType.PassingTimeFirstArrivalTimeError to "first passing time must not have arrival_time set",
HasuraErrorType.PassingTimeLastDepartureTimeError to "last passing time must not have departure_time set",
HasuraErrorType.PassingTimeNullError to "all passing time that are not first or last in the sequence must have both departure and arrival time defined",
HasuraErrorType.PassingTimesMixedJourneyPatternRefsError to "inconsistent journey_pattern_ref within vehicle journey, all timetabled_passing_times must reference same journey_pattern_ref as the vehicle_journey",
HasuraErrorType.ConflictingSchedulesError to "validate_queued_schedules_uniqueness",
HasuraErrorType.SequentialIntegrityError to "validate_service_sequential_integrity"
TimetablesApiErrorType.PassingTimeStopPointMatchingOrderError to "passing times and their matching stop points must be in same order",
TimetablesApiErrorType.PassingTimeFirstArrivalTimeError to "first passing time must not have arrival_time set",
TimetablesApiErrorType.PassingTimeLastDepartureTimeError to "last passing time must not have departure_time set",
TimetablesApiErrorType.PassingTimeNullError to "all passing time that are not first or last in the sequence must have both departure and arrival time defined",
TimetablesApiErrorType.PassingTimesMixedJourneyPatternRefsError to "inconsistent journey_pattern_ref within vehicle journey, all timetabled_passing_times must reference same journey_pattern_ref as the vehicle_journey",
TimetablesApiErrorType.ConflictingSchedulesError to "validate_queued_schedules_uniqueness",
TimetablesApiErrorType.SequentialIntegrityError to "validate_service_sequential_integrity"
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fi.hsl.jore4.timetables.api.util

enum class HasuraErrorType {
enum class TimetablesApiErrorType {
UnknownError,
ConflictingSchedulesError,
InvalidTargetPriorityError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TransactionSystemExtensionsTest {
""".trimIndent()
)
)
assertEquals(exception.type, HasuraErrorType.PassingTimeStopPointMatchingOrderError)
assertEquals(exception.type, TimetablesApiErrorType.PassingTimeStopPointMatchingOrderError)
}

@Test
Expand All @@ -33,7 +33,7 @@ class TransactionSystemExtensionsTest {
""".trimIndent()
)
)
assertEquals(exception.type, HasuraErrorType.PassingTimeFirstArrivalTimeError)
assertEquals(exception.type, TimetablesApiErrorType.PassingTimeFirstArrivalTimeError)
}

@Test
Expand All @@ -46,7 +46,7 @@ class TransactionSystemExtensionsTest {
""".trimIndent()
)
)
assertEquals(exception.type, HasuraErrorType.PassingTimeLastDepartureTimeError)
assertEquals(exception.type, TimetablesApiErrorType.PassingTimeLastDepartureTimeError)
}

@Test
Expand All @@ -59,7 +59,7 @@ class TransactionSystemExtensionsTest {
""".trimIndent()
)
)
assertEquals(exception.type, HasuraErrorType.PassingTimeNullError)
assertEquals(exception.type, TimetablesApiErrorType.PassingTimeNullError)
}

@Test
Expand All @@ -72,7 +72,7 @@ class TransactionSystemExtensionsTest {
""".trimIndent()
)
)
assertEquals(exception.type, HasuraErrorType.PassingTimesMixedJourneyPatternRefsError)
assertEquals(exception.type, TimetablesApiErrorType.PassingTimesMixedJourneyPatternRefsError)
}

@Test
Expand All @@ -85,7 +85,7 @@ class TransactionSystemExtensionsTest {
""".trimIndent()
)
)
assertEquals(exception.type, HasuraErrorType.ConflictingSchedulesError)
assertEquals(exception.type, TimetablesApiErrorType.ConflictingSchedulesError)
}

@Test
Expand All @@ -98,7 +98,7 @@ class TransactionSystemExtensionsTest {
""".trimIndent()
)
)
assertEquals(exception.type, HasuraErrorType.SequentialIntegrityError)
assertEquals(exception.type, TimetablesApiErrorType.SequentialIntegrityError)
}

@Test
Expand All @@ -111,6 +111,6 @@ class TransactionSystemExtensionsTest {
""".trimIndent()
)
)
assertEquals(exception.type, HasuraErrorType.TransactionSystemError)
assertEquals(exception.type, TimetablesApiErrorType.TransactionSystemError)
}
}

0 comments on commit b11ca33

Please sign in to comment.