spells.ThrowableOpsModule.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spells_2.11 Show documentation
Show all versions of spells_2.11 Show documentation
This is a small scala "util" library, which will hopefully grow over time.
The newest version!
package spells
/** Contains utility methods for `Throwable`s. */
trait ThrowableOpsModule {
this: AnsiModule with CustomRenderingModule with SpellsConfigModule with AnsiModule with StringOpsModule with StylePrintModule =>
import java.io.{ PrintWriter, StringWriter }
implicit final class ThrowableOpsFromSpells(value: Throwable) extends CustomRendering {
override final def rendered(implicit availableWidthInCharacters: StringOpsModule#AvailableWidthInCharacters = SpellsConfig.terminal.WidthInCharacters.value): String =
styled(getFullStackTraceString)(AnsiStyle.Red)
final def getFullStackTraceString: String = {
val writer = new StringWriter
val autoFlush = true
value.printStackTrace(new PrintWriter(writer, autoFlush))
writer.getBuffer.deleteCharAt(writer.getBuffer.length - 1).toString.replace("\t", " ")
}
/** Gets the deepest `Throwable`.
* @return the root cause
*/
@scala.annotation.tailrec
final def getRootCause: Throwable =
if (value.getCause == null) value
else value.getCause.getRootCause
}
}