Architecture #14870

Updated by Fran├žois ARMAND almost 2 years ago

 *Context: LiftWeb Box* 

 Historically, we used @liftweb@ @Box@ `liftweb` `Box` type to handle result which may fail in Rudder.  

 This was good and quite on the edge 10 years ago, and `Box` usage brings two majors features:  

 - a) @Box@ `Box` clearly split appart the nominal path (@Full[A]@) (`Full[A]`) from errors (@Failure@). (`Failure`). In that regard, @Box@    `Box` is the same than @Either@ `Either` or any modern @IO@ `IO` monad (but remember that at the time, scala @Either@ `Either` wasn't right-biased...)   
 - b) and more importantly, errors come with the possiblity to iteratively build up explanation, giving more context depending where the error happen and chaining user-oriented message.  

 On that second point, it means that you can "chain" explanation, so that each layer can set the relevant context for the error, typically from very technical for lower layer, and more user-oriented in higher one. These stack of messages can then be formatted and displayed depending of the targetted audience.  

 For example, imagine that your UI did an ajax request to get some details on something, and the database is down at that moment.  
 The user oriented message then can be read as: @There "There was a problem with data access, please retry or contact your administrator@ administrator" while the log display: 
 [.....] There was a problem with data access, please retry or contact your administrator <- JSON request to URL .... returned an error <- Impossible to get details for configuration with ID xxxxx <- Connexion to database error: (technical details of why)" 

 This is a very good, and extremely important feature for a project like Rudder. So we use it pervasively in Rudder code (> 120k usage in Rudder 5.0). 

 *Box limitations* 

 Nonetheless, @Box@ `Box` has three major problems:  

 - 1/ it's a lift dependency, and lift is web-framework oriented, and not much used beside that case. We would prefer to have an effect/error lib for that (and bridge it with `Box` in the web part). This one is not a breaking one, though. 

 - @Box@ `Box` is tri-stated: @Full[A]@, @Empty@, `Full[A]`, `Empty`, and @Failure@, `Failure`, and 10 years of usage lead us to believe that even with the stricter discipline, @Empty@ `Empty` semantic is impossible to maintain in time. We never know if it's a non-explained error, or if it is a expected empty case. The semantic of @flatMap@ `flatMap` let us believe it's the first interpretation, but actually, we never want to have not contextualised errors. So that state is more of a burden than an help (we still have to deal with it even if we banned it), and contribute to massive puzzling for new commers.  

 - and more specifically, `Box` is not at all a principled effect management library, with verified applicative/monad laws, and helpful combinators.  

 *Introducing ZIO* 

 @ZIO@ `ZIO` answer the three draw backs of `Box`, because obviously it is a principled effect library, developped with 10 years of R&D evolution on the topic, even pushing the state of the art on it.   

 It goes even way further than what provided @Box@, `Box`, bringing a whole new world of async and concurrent programing, @Schedule@, @Software `Schedule`, `Software Transactionnal Memory@, Memory`, efficient purelly functionnal queues, etc etc. We were forced to used some other libraries for these topics (like `monix`), which multiplies the concept and dependencies.   

 But @ZIO@ `ZIO` doesn't help directly for the contextualisation of errors. At least, it helps a lot for the developper, with an extremelly powerful tracing framework: 

 But (obviously) it does not help for what are `Result` in the context of @Rudder@. `Rudder`. So I build one with the @RudderError@, @PureResult@ `RudderError`, `PureResult` and @IOResult@ `IOResult` hierachies.  

 *Povided in that evolution* 

 The PR corresponding to that evolution introduces 3 things:  

 * 1/ the concept of @RudderError@ `RudderError` which is the main type of errors in Rudder.  

 It provides common error cases and combinators. Notably, the defaults errors are:  

 - @SystemError@ "SystemError" which encapsulate exception,  
 - @Unexpected@ "Unexpected" for value that should not happens for example when building a config,  
 - @Unconsistant@ "Unconsistant" for things that should not happen from a business perspective like "that entity should really be in the base because I just checked";  
 - @Chained@ `Chained` error case which provides a ".chainError" combinator to (yes, suspens) chain an error with a new contextualised message 
 - And an @Accumuted@ `Accumuted` error for the Applicative accumulation of errors.   

 All Rudder errors implement a @.msg@ "msg" method that allows for user rendering of the method, with the correct logic for each of them (particulary Chained / Accumulated ones).  

 These basic cases come with combinators like @.notOptionnal@, @.chainError@, @.accumulate@... ".notOptionnal", ".chainError", ".accumulate".  

 * 2/ the concept of `Result`, with a pure and an effect variant. The pure variant is an alias to @Either[RudderError, A]@ `Either[RudderError, A]` and is dedicated to non effectful code (ie: no execption, no I/O, no random, no "System time millis" etc) but with perhaps a business error. The effect variant is (TADAAAA) dedicated to effectful computation (java bridge that can throw error, I/O, etc) which is an alias to @ZIO[Any, RudderError, A]@ - we don't use @ZIO@ context for now. etc).  

 * 3/ and of course, ZIO as our effect library.  

 It also provides combinators to go from @Box@ `Box` to/from @Result@, `Result`, and port a substantial part of Rudder lower level to @ZIO@. `ZIO`. 

 The main set-up is available in: 

 That basic framework also allows to have specialized domain error for a dedicated part of the application. I did it for LDAP connection module (also: technique parsing; entity mapping), as we want to manage LDAP errors with a more precise semantic than what provides the base errors described above. 

 This can be seen here: