EG-2684 Ensure original message is logged with old-style error codes (#6340)

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

* [EG-2684] Address detekt issues
This commit is contained in:
James Higgs 2020-06-12 16:52:01 +01:00 committed by GitHub
parent ef00fa1388
commit 2c253d806d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View File

@ -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
}

View File

@ -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)
}