diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-01-06 13:41:20 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-01-06 13:41:20 +0100 |
commit | 6b316ac648963407bfd58c3465a999acc8854a23 (patch) | |
tree | c63667a41664cdf1b811e64ad69f5baead4fa3b6 /vespajlib | |
parent | 2f50f82439d19e815306b99c98003d02d6c97ccc (diff) |
Add ExceptionUtils to vespajlib
Diffstat (limited to 'vespajlib')
-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 |