Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add printStackTrace to Console #1897

Merged
merged 4 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions std/shared/src/main/scala/cats/effect/std/Console.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ trait Console[F[_]] { self =>
*/
def errorln[A](a: A)(implicit S: Show[A] = Show.fromToString[A]): F[Unit]

/**
* Prints the stack trace of the given Throwable to standard error output.
*/
def printStackTrace(t: Throwable): F[Unit]

/**
* Modifies the context in which this console operates using the natural
* transformation `f`.
Expand All @@ -147,6 +152,9 @@ trait Console[F[_]] { self =>

def errorln[A](a: A)(implicit S: Show[A]): G[Unit] =
f(self.errorln(a))

def printStackTrace(t: Throwable): G[Unit] =
f(self.printStackTrace(t))
}
}

Expand Down Expand Up @@ -320,5 +328,8 @@ object Console {
val text = a.show
F.blocking(System.err.println(text))
}

def printStackTrace(t: Throwable): F[Unit] =
F.blocking(t.printStackTrace())
}
}
15 changes: 15 additions & 0 deletions tests/shared/src/test/scala/cats/effect/std/ConsoleSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,20 @@ class ConsoleSpec extends BaseSpec {
}
}

"printStackTrace to the standard error output" in real {
val e = new Throwable("error!")

val stackTraceString =
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we can reliably test this that way: I suppose the representation of a stack trace on the JVM could vary between versions... maybe it's a good idea to keep it while it works, and remove if there are any issues in the future? Or should we just drop it now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we'll first find out whether it breaks between JDK 8 and 11.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, I think it's fine to just check that the custom message that we do control is present on the err stream.

e.getStackTrace()
.map { line => "\tat " + line.toString }
.mkString(e.toString + "\n", "\n", "\n")

standardErrTest(Console[IO].printStackTrace(e)).flatMap { err =>
IO {
err must beEqualTo(stackTraceString)
}
}
}

}
}