Skip to content

Commit

Permalink
EG-2684 Ensure original message is logged with old-style error codes (c…
Browse files Browse the repository at this point in the history
…orda#6340)

* [EG-2684] Ensure original message is logged with old-style error codes

* [EG-2684] Address detekt issues
  • Loading branch information
JamesHR3 authored Jun 12, 2020
1 parent ef00fa1 commit 2c253d8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ fun Message.withErrorCodeFor(error: Throwable?, level: Level): Message {

return when {
error != null && level.isInRange(Level.FATAL, Level.WARN) -> {
val logMessage = this.formattedMessage
val message = error.walkExceptionCausedByList().asSequence().mapNotNull(Throwable::message).joinToString(" - ")
CompositeMessage("$message [errorCode=${error.errorCode()}, moreInformationAt=${error.errorCodeLocationUrl()}]", format, parameters, throwable)
CompositeMessage("$logMessage - $message [errorCode=${error.errorCode()}, moreInformationAt=${error.errorCodeLocationUrl()}]", format, parameters, throwable)
}
else -> this
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.corda.commmon.logging

import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.contains
import net.corda.common.logging.withErrorCodeFor
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.message.SimpleMessage
import org.junit.Test
import kotlin.test.assertEquals

class ExceptionsErrorCodeFunctionsTest {

@Test(timeout=3_000)
fun `error code for message prints out message and full stack trace`() {
val originalMessage = SimpleMessage("This is a test message")
var previous: Exception? = null
val throwables = (0..10).map {
val current = TestThrowable(it, previous)
previous = current
current
}
val exception = throwables.last()
val message = originalMessage.withErrorCodeFor(exception, Level.ERROR)
assertThat(message.formattedMessage, contains("This is a test message".toRegex()))
for (i in (0..10)) {
assertThat(message.formattedMessage, contains("This is exception $i".toRegex()))
}
assertEquals(message.format, originalMessage.format)
assertEquals(message.parameters, originalMessage.parameters)
assertEquals(message.throwable, originalMessage.throwable)
}

private class TestThrowable(index: Int, cause: Exception?) : Exception("This is exception $index", cause)
}

0 comments on commit 2c253d8

Please sign in to comment.