Zionomicon
-
bought: 2020-10-29
- Zionomicon: content
- 4.10 Execution Tracing
-
Diagnosing failures in Future-based code is notoriously difficult, which is because the stack traces generated by Future code are not very helpful. Rather than showing the surrounding context, they show implementation details deep inside the implementation of Future.
-
To deal with this problem, ZIO includes a feature called execution tracing, which provides extremely fine-grained details on the context surrounding failures. Execution tracing is turned on by default, and allows you to more quickly track down the root cause of problems in your code.
-
Because ZIO’s error channel is polymorphic, you can use your own data types there, and ZIO has no way to attach execution tracing to unknown types. As a result, to retrieve the execution trace of an error, you need the full Cause. Once you have the cause, you can simply call prettyPrint to convert the full cause information, including execution tracing, into a human-readable string.
-
In the following code snippet, the full cause data is logged to the console in the event of a failure:
-
lazy val effect: ZIO[Any, IOException, String] = ???
effect.tapCause(cause => console.putStrLn(cause.prettyPrint))
Note the method ZIO#tapCause
is used, which allows us to “tap into” the cause of a failure, effectively doing something on the side (like logging) without changing the success or failure of the effect.
- 14 Resource handling