-
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.
Pivotal ID # 186629008: Slack Message For Failed Notifications (#794)
https://www.pivotaltracker.com/story/show/186629008 - Add message converter to the rabbit template - Configure custom exception handler to catch failing messages
- Loading branch information
1 parent
75b2c28
commit ea53168
Showing
5 changed files
with
104 additions
and
2 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
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
25 changes: 25 additions & 0 deletions
25
...ission-handlers/src/main/kotlin/ac/uk/ebi/biostd/handlers/exception/CustomErrorHandler.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,25 @@ | ||
package ac.uk.ebi.biostd.handlers.exception | ||
|
||
import ac.uk.ebi.biostd.handlers.common.HANDLERS_SUBSYSTEM | ||
import ac.uk.ebi.biostd.handlers.common.SYSTEM_NAME | ||
import ebi.ac.uk.commons.http.slack.Alert | ||
import ebi.ac.uk.commons.http.slack.NotificationsSender | ||
import kotlinx.coroutines.runBlocking | ||
import org.springframework.amqp.AmqpRejectAndDontRequeueException | ||
import org.springframework.amqp.rabbit.listener.ConditionalRejectingErrorHandler | ||
|
||
class CustomErrorHandler( | ||
private val notificationsSender: NotificationsSender, | ||
) : ConditionalRejectingErrorHandler() { | ||
override fun handleError(t: Throwable) { | ||
runBlocking { | ||
notificationsSender.send(Alert(SYSTEM_NAME, HANDLERS_SUBSYSTEM, "$ERROR_MESSAGE: ${t.localizedMessage}")) | ||
} | ||
|
||
throw AmqpRejectAndDontRequeueException(t) | ||
} | ||
|
||
companion object { | ||
internal const val ERROR_MESSAGE = "Problem processing rabbit message for notification" | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...on-handlers/src/test/kotlin/ac/uk/ebi/biostd/handlers/exception/CustomErrorHandlerTest.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,41 @@ | ||
package ac.uk.ebi.biostd.handlers.exception | ||
|
||
import ac.uk.ebi.biostd.handlers.common.HANDLERS_SUBSYSTEM | ||
import ac.uk.ebi.biostd.handlers.common.SYSTEM_NAME | ||
import ac.uk.ebi.biostd.handlers.exception.CustomErrorHandler.Companion.ERROR_MESSAGE | ||
import ebi.ac.uk.commons.http.slack.Alert | ||
import ebi.ac.uk.commons.http.slack.NotificationsSender | ||
import io.mockk.coEvery | ||
import io.mockk.coVerify | ||
import io.mockk.every | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.junit5.MockKExtension | ||
import io.mockk.slot | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.springframework.amqp.AmqpRejectAndDontRequeueException | ||
import kotlin.test.assertFailsWith | ||
|
||
@ExtendWith(MockKExtension::class) | ||
class CustomErrorHandlerTest( | ||
@MockK private val throwable: Throwable, | ||
@MockK private val notificationsSender: NotificationsSender, | ||
) { | ||
private val testInstance = CustomErrorHandler(notificationsSender) | ||
|
||
@Test | ||
fun `handle error`() { | ||
val alertSlot = slot<Alert>() | ||
val expectedAlert = Alert(SYSTEM_NAME, HANDLERS_SUBSYSTEM, "$ERROR_MESSAGE: the error message") | ||
|
||
every { throwable.localizedMessage } returns "the error message" | ||
coEvery { notificationsSender.send(capture(alertSlot)) } answers { nothing } | ||
|
||
assertFailsWith<AmqpRejectAndDontRequeueException> { testInstance.handleError(throwable) } | ||
|
||
val alert = alertSlot.captured | ||
assertThat(alert).isEqualToComparingFieldByField(expectedAlert) | ||
coVerify(exactly = 1) { notificationsSender.send(alert) } | ||
} | ||
} |