Skip to content

Commit

Permalink
Handle exception gracefully
Browse files Browse the repository at this point in the history
Signed-off-by: bowenlan-amzn <[email protected]>
  • Loading branch information
bowenlan-amzn committed Oct 12, 2023
1 parent b5b987c commit dbd9b0f
Showing 1 changed file with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -438,16 +438,7 @@ object ManagedIndexRunner :
var executedManagedIndexMetaData = startingManagedIndexMetaData.getCompletedManagedIndexMetaData(action, step)

if (executedManagedIndexMetaData.isFailed) {
try {
// if the policy has no error_notification this will do nothing otherwise it will try to send the configured error message
publishErrorNotification(policy, executedManagedIndexMetaData)
} catch (e: Exception) {
logger.error("Failed to publish error notification", e)
val errorMessage = e.message ?: "Failed to publish error notification"
val mutableInfo = executedManagedIndexMetaData.info?.toMutableMap() ?: mutableMapOf()
mutableInfo["errorNotificationFailure"] = errorMessage
executedManagedIndexMetaData = executedManagedIndexMetaData.copy(info = mutableInfo.toMap())
}
executedManagedIndexMetaData = publishErrorNotification(policy, executedManagedIndexMetaData)

Check warning on line 441 in src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt#L441

Added line #L441 was not covered by tests
}

if (executedManagedIndexMetaData.isSuccessfulDelete) {
Expand Down Expand Up @@ -816,13 +807,27 @@ object ManagedIndexRunner :
}
}

private suspend fun publishErrorNotification(policy: Policy, managedIndexMetaData: ManagedIndexMetaData) {
policy.errorNotification?.run {
errorNotificationRetryPolicy.retry(logger) {
val compiledMessage = compileTemplate(messageTemplate, managedIndexMetaData)
destination?.buildLegacyBaseMessage(null, compiledMessage)?.publishLegacyNotification(client)
channel?.sendNotification(client, ErrorNotification.CHANNEL_TITLE, managedIndexMetaData, compiledMessage, policy.user)
private suspend fun publishErrorNotification(policy: Policy, metadata: ManagedIndexMetaData): ManagedIndexMetaData {
return try {

Check warning on line 811 in src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt#L811

Added line #L811 was not covered by tests
// if the policy has no error_notification this will do nothing otherwise it will try to send the configured error message
policy.errorNotification?.run {
errorNotificationRetryPolicy.retry(logger) {
val compiledMessage = compileTemplate(messageTemplate, metadata)

Check warning on line 815 in src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt#L814-L815

Added lines #L814 - L815 were not covered by tests
destination?.buildLegacyBaseMessage(null, compiledMessage)?.publishLegacyNotification(client)
channel?.sendNotification(client, ErrorNotification.CHANNEL_TITLE, metadata, compiledMessage, policy.user)
}
}
val message = "Successfully published error notification [index = ${metadata.index}]"
logger.info(message)

Check warning on line 821 in src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt#L820-L821

Added lines #L820 - L821 were not covered by tests
val mutableInfo = metadata.info?.toMutableMap() ?: mutableMapOf()
mutableInfo["error_notification"] = message
metadata.copy(info = mutableInfo.toMap())
} catch (e: Exception) {
logger.error("Failed to publish error notification", e)

Check warning on line 826 in src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt#L823-L826

Added lines #L823 - L826 were not covered by tests
val errorMessage = e.message ?: "Failed to publish error notification"
val mutableInfo = metadata.info?.toMutableMap() ?: mutableMapOf()
mutableInfo["error_notification"] = errorMessage
metadata.copy(info = mutableInfo.toMap())

Check warning on line 830 in src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt#L829-L830

Added lines #L829 - L830 were not covered by tests
}
}

Expand Down

0 comments on commit dbd9b0f

Please sign in to comment.