CE-847 Add method getTopAndBottomMessages

This commit is contained in:
2024-02-12 18:54:53 -06:00
parent b8f9469477
commit 718701aaff
2 changed files with 32 additions and 0 deletions

View File

@ -164,4 +164,23 @@ public class ExceptionUtils
return (StringUtils.join("; ", messages)); return (StringUtils.join("; ", messages));
} }
/*******************************************************************************
** Get the messages from the top & bottoms (root) of an exception.
**
** If there's no root, just return the top (e.g., parameter)'s message.
** If they are both found, put ": " between them.
*******************************************************************************/
public static String getTopAndBottomMessages(Exception e)
{
String rs = e.getMessage();
Throwable rootException = getRootException(e);
if(rootException != e)
{
rs += ": " + rootException.getMessage();
}
return (rs);
}
} }

View File

@ -116,6 +116,19 @@ class ExceptionUtilsTest extends BaseTest
/*******************************************************************************
**
*******************************************************************************/
@Test
void testGetTopAndBottomMessages()
{
assertEquals("foo", ExceptionUtils.getTopAndBottomMessages(new Exception("foo")));
assertEquals("foo: bar", ExceptionUtils.getTopAndBottomMessages(new Exception("foo", new Exception("bar"))));
assertEquals("foo: baz", ExceptionUtils.getTopAndBottomMessages(new Exception("foo", new Exception("bar", new Exception("baz")))));
}
/******************************************************************************* /*******************************************************************************
** Test exception class - lets you set the cause, easier to create a loop. ** Test exception class - lets you set the cause, easier to create a loop.
*******************************************************************************/ *******************************************************************************/