diff options
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/exception/ExceptionUtils.java | 40 | ||||
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/exception/package-info.java | 8 |
2 files changed, 48 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/exception/ExceptionUtils.java b/vespajlib/src/main/java/com/yahoo/exception/ExceptionUtils.java new file mode 100644 index 00000000000..4d4a2d02a1a --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/exception/ExceptionUtils.java @@ -0,0 +1,40 @@ +// Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.exception; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.io.UncheckedIOException; + +/** + * Misc exception utility methods - replacement for Apache commons-lang's ExceptionUtils + * + * @author bjorncs + */ +public class ExceptionUtils { + + private ExceptionUtils() {} + + public static String getStackTraceAsString(Throwable throwable) { + try (StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter, true)) { + throwable.printStackTrace(printWriter); + return stringWriter.getBuffer().toString(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + public static String getStackTraceRecursivelyAsString(Throwable throwable) { + Throwable cause = throwable; + try (StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter, true)) { + do { + cause.printStackTrace(printWriter); + } while ((cause = cause.getCause()) != null); + return stringWriter.getBuffer().toString(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } +} diff --git a/vespajlib/src/main/java/com/yahoo/exception/package-info.java b/vespajlib/src/main/java/com/yahoo/exception/package-info.java new file mode 100644 index 00000000000..8254f90651f --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/exception/package-info.java @@ -0,0 +1,8 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +/** + * @author bjorncs + */ +@ExportPackage +package com.yahoo.exception; + +import com.yahoo.osgi.annotation.ExportPackage;
\ No newline at end of file |