TutorialsAlpha

In the previous section of this tutorial, we learned how to write simple programs with Effect. In this section, we will expand upon that knowledge to learn about managing errors with Effect.

Two Types of Errors

Just like any other program, Effect programs may fail for expected or unexpected reasons. The difference between a non-Effect program and an Effect program is in the detail provided to you when your program fails. Effect attempts to preserve as much information as possible about what caused your program to fail to produce a detailed, comprehensive, and human readable failure message.

In an Effect program, there are two possible ways for a program to fail:

  • Expected Errors: These are errors that developers anticipate and expect as part of normal program execution
  • Unexpected Errors: These are errors that occur unexpectedly and are not part of the intended program flow

Expected Errors

These errors, also referred to as failures, typed errors or recoverable errors, are errors that a developer anticipates as part of the normal program execution. They serve a similar purpose to checked exceptions and play a role in defining the program's domain and control flow.

As we learned in a previous section of this tutorial, expected errors are tracked at the type level by the Effect data type (in the Error channel).

For example, it is evident from the type that the Effect program in the following code snippet can fail with an error of type HttpError or an error of type NetworkError:

Effect<string, HttpError | NetworkError>

Before continuing with this tutorial, please read through the section on expected errors in the documentation.

Unexpected Errors

Unexpected errors, also referred to as defects, untyped errors, or unrecoverable errors, are errors that developers do not anticipate occurring during normal program execution. Unlike expected errors, which are considered part of a program's domain and control flow, unexpected errors resemble unchecked exceptions and lie outside the expected behavior of the program.

Since these errors are not expected, Effect does not track them at the type level. However, the Effect runtime does keep track of these errors and provides several methods to aid in recovering from unexpected errors.

Before continuing with this tutorial, please read through the section on unexpected errors in the documentation.

Exercise

Using the concepts we just learned about managing errors with Effect, complete the following tasks (also marked with TODOs in the editor):

  • Create a custom HttpError class with a _tag attribute that is set to the value "HttpError"
  • Create a custom NetworkError class with a _tag attribute that is set to the value "NetworkError"
  • Add the necessary code to the pipeline to retry the program on errors for a maximum of three times, but only if the error is an HttpError
  • Add the necessary code to the pipeline to transform all expected errors remaining after the retries into defects

Bonus: Use the Data module to create the custom error types with Data.TaggedError.

Remember, if you ever get stuck try clicking the "Show Solution" button in the upper right-hand corner of the editor.

Next: Managing Requirements