Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@khajavi
Copy link
Member

@khajavi khajavi commented Feb 1, 2022

@khajavi khajavi changed the base branch from master to series/2.x February 1, 2022 12:26
@jdegoes
Copy link
Member

jdegoes commented Feb 3, 2022

Worth looking at:

https://gist.github.com/quelgar/d383cd960d781fe72a1564203c280a03

As it addresses many topics important to consider in this phase of the documentation.

@khajavi
Copy link
Member Author

khajavi commented Feb 3, 2022

Worth looking at:

https://gist.github.com/quelgar/d383cd960d781fe72a1564203c280a03

As it addresses many topics important to consider in this phase of the documentation.

Great article! Yes, I noticed that from your tweets.

@khajavi
Copy link
Member Author

khajavi commented Feb 4, 2022

While documenting union types on ZIO's error parameter, a question popped into my head. Why the compiler does not infer the proper type for myApp? It should be ZIO[Any, FooError | BarError, Unit], is this true?

// Two unrelated errors without having a common supertype (e.g. FooBarError) or the Throwable/Exception supertype
trait FooError
trait BarError

// Why the compiler doesn't infer the error type as `FooError | BarError`?
val myApp: ZIO[Any, Object, Unit] = for {
  _ <- ZIO.fail(new FooError {})
  _ <- ZIO.fail(new BarError {})
} yield ()

@jdegoes @adamgfraser


Update: I found that it is the inherent behavior of the Scala 3's type system related to the LUB algorithm and is not related to ZIO itself.

@edubrovski
Copy link

edubrovski commented Feb 4, 2022

Hi @khajavi , could you please add some description of resurrect method, since it's closely related to orDie? Here's how I understand the usage of orDie + resurrect:

Sometimes we want to turn a ZIO[R, E, A] into ZIO[R, Nothing, A]. An unexpected database failure can be an example of this. Let's say I'm using MongoDB with mongo-scala-driver. mongo-scala-driver returns scala.concurrent.Future which I need to somehow turn into ZIO[R, Nothing, A], because callers of data access layer don't need to know about unexpected Mongo failures. I call Task.fromFuture(...).orDie to get rid of the Throwable.

Somewhere in my application I have a generic top level handler which does something like

.resurrect
.catchAll {
  case unexpected: Throwable => log.error(unexpected) *> ZIO.succeed(InternalServerError)
}

Is this the right usage of resurrect? I think it should be documented. I'm having a hard time trying to persuade my coworkers to use orDie + resurrect. They're concerned about killing the fiber. I wonder how resurrect works internally, when ZIO is run. Is it something like

catch {
  case ex: InterruptedException => ...
}

?

Also, is it possible to add a section saying something like "don't be lazy, encode errors with ADTs instead of using Throwable as an E"? I'm currently working on a codebase where everything is a ZIO[R, Throwable, A] and for typed errors something like ZIO[R, Throwable, Either[DomainError, A]] is used 😃

@khajavi
Copy link
Member Author

khajavi commented Feb 8, 2022

@edubrovski
Thank you for your note, helping the documentation becomes better.

I'm not sure this is a use case for resurrect, if those errors are unexpected errors, why not use ZIO#catchAllDefect?

@edubrovski
Copy link

@khajavi,
thanks for pointing me to catchAllDefect, I didn't know it existed. I think it makes sense to add a couple of lines about catchAllDefect then. Currently the doc doesn't explain how to actually handle defects after we create them with orDie. In most real life cases we want to handle them, not simply dump them to stdout. Even if a defect is completely unexpected and we're letting a piece of code crash, we still might want to:

  • log it nicely by sending it to a log aggregator like Splunk
  • send an email alert to developers
  • display a nice "unexpected error" message to the user
  • etc.

@jdegoes
Copy link
Member

jdegoes commented Feb 11, 2022

@khajavi

In theory, if we changed flatMap to return E1 | E2, then it should work. This is probably worth thinking about for 2.0.

@khajavi khajavi force-pushed the error-handling-documentation branch from 9ce4feb to b6a400f Compare March 7, 2022 12:43
@jdegoes
Copy link
Member

jdegoes commented Mar 11, 2022

@khajavi Are you done with this one?

@khajavi
Copy link
Member Author

khajavi commented Mar 11, 2022

@jdegoes
There are still a few topics to cover, but it is almost done.
If you have any thoughts on this, please let me know.

@khajavi khajavi force-pushed the error-handling-documentation branch from afe5ddf to 80e2ce2 Compare March 24, 2022 14:58
@khajavi khajavi marked this pull request as ready for review March 25, 2022 08:04
@khajavi
Copy link
Member Author

khajavi commented Mar 25, 2022

@jdegoes
This work is ready for your review.

@khajavi khajavi force-pushed the error-handling-documentation branch from 0eef517 to 8a7bdbd Compare April 1, 2022 16:29
Copy link

@mbbx6spp mbbx6spp left a comment

Choose a reason for hiding this comment

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

I found the docs on Sandboxing useful and just wanted to check if there was a typo with inline queries. Thanks.

khajavi added 4 commits May 5, 2022 14:32
# Conflicts:
#	docs/datatypes/core/zio/zio.md
# Conflicts:
#	docs/datatypes/core/zio/zio.md
### 3. Fatal Errors

In ZIO, the `VirtualMachineError` and all its subtypes are the only errors considered fatal by the ZIO runtime. So if during the running application, the JVM throws any of these errors like `StackOverflowError`, the ZIO runtime considers it as a catastrophic fatal error. So it will interrupt the whole application immediately without safe resource interruption. None of the `ZIO#catchAll` and `ZIO#catchAllDefects` can catch these fatal errors. At most, if the `RuntimeConfig.reportFatal` is enabled, the application will log the stack trace before interrupting the whole application.

Copy link

@tperrigo tperrigo May 5, 2022

Choose a reason for hiding this comment

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

I believe RuntimeConfig (and RuntimeConfigAspect) have been deleted.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am making things challenging for @khajavi here. 😃

Copy link
Member Author

Choose a reason for hiding this comment

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

No problem, I'm keeping the branch up to date regularly ;)

@adamgfraser
Before the removal of RuntimeConfig, we had the ability to specify fatal errors:

val c: RuntimeConfig = ???
c.fatal { case x: Throwable => x.isInstanceOf[Foo] }

But I can't find the corresponding method in the new version. We won't support doing so anymore?

@atooni atooni mentioned this pull request Jun 24, 2022
6 tasks
@khajavi khajavi closed this Jul 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants