scale.bythebay.io: Haeley Yao, Error Handling Without Throwing Exceptions
Recording: scale.bythebay.io: Haeley Yao, Error Handling Without Throwing Exceptions
you hello everyone I'm Haley I give a talk today about error handling results the exception thank you for coming in my talk in this Saturday afternoon little bit brave introduce about myself I live in Oakland I like Oakland I like the tray in Oakland especially the oak tree in your yard we have a red oak tree it's big you can see from picture and one day a cat come to visit our yard it's kind hard to find it it's in the center of Greek cat eye coding Scala for more than three years car I'm really like function programming I'm keep on trying to learning and practice the functional programming idea in my project I walk in eBay now our team is called a new product new product develop team because MPD previously I work with kabobs herbs I work with in my during my experience I will come build the data pipeline and build web apps and work on infrastructure about the project I work on eBay we work on the Shabbat build the Shabbat it's a chatbot to help people to shopping more easily and also now we work on building mobile app was a Chinese customer to help them to buy the import product from us and from other country international my during my work project we'll use the play framework we use casts we use enumerator in your numerator also use a library called scala scraper to do the hm HTML parsing and for camping management we use the types of camping and also your step zip logging we save our data in data in BigTable Google BigTable for the testing wheel Scala tests and mockito to mock the big data to do the to do the unit tests for the phone for the function like talk given by the job nearest yesterday he said a function is is a pure function or has a side effect a function has that effect if it's a party or if ception or if non determing or if have a dependency injection or do some logging or milk mutate the variable so we have in our code base previously we have lots of we lots of succession how this what we do to handle the error so that is that is a not not a pure function because so when so exception that breaks the definition of the function for functions they define from from A to B that means for every value from a you will get a value in the P but when you so suction functions that means sometimes you'll get something some exception so it's not pure it's a partial function the the partial function is bad because when we were handling the error if waste exception in when errors happen and in some place in the high level we still need handle this error or handle this error so from the low level and this error is is this function is not pure is partial so we cannot compose them that makes a high level function the logic is very complex and it's hard to how to read that's the reason we try to reflect our code and carried all this subsection and make our code error handling with is more in functional way and so makes the our function can composable every Lincoln composable with this summer research on how to do that with which heís a couple of alternatives to deal with that like in Scala we can we can use option who can use try we can use either option is if we have a good value okay we retain it which is something some if ever happen we just return now the the best thing is we don't know what error if it's a lot of error windows is this information so either is good because either can keeps a good value and a better value we can know what error and we also can returns a good value we also try scholars a because scholars say we can use disjunction to increment the phosphate error catch the phosphate error in the error handling we have a handle two different the two different kind of error one is the phosphate that means in the whole process if any function so error wait just stop it and another one is we try to catch all the error in all the function it's like but cumulative in scholars a we can use disjunction to handle the phosphate error handling and use a validation to handle a cumulative error we also try the Scala galactic use one for phosphate error and use many or accumulation this library to do the accumulate accumulate error and in casts we can use either for phosphate and validated for the for the accumulated error we this example we did a try a simple example is we did this example we start from this example is given a string and we this drink hence use information and username and the user age and we try to gather past the username and also we try to pass a user age is integer and positive integer so in the first version we do is we use a so exception as we did before and then we try the version use a use option the we return option and how in when the handles one day we can use a for comprehension to increment this phosphate and also we try we use a try we return try and try again return good value and better value so we put in the for comprehension is the same and we can do the phosphate the similar idea and this either it's a little bit longer I pull so here you can check it later and also we have all the tests to test all the case all the test is passed I can share the I can write later ok let me go back this alternative we will try and the in our project is we handle in a project we we work with put we build a service and this service will call lots of act on a service and the one we call external service the behavior in a synchronous way that means the world were returning our code return future everywhere and also we want is when any external service have error happen we want to catch that and we want to know which actual service has error and then we can try to debug that or we can find out we can go deep to deposit that external service so we need stacking the future and either together because either can tell us good value and better value and we want working with the future so we do that is we use a cast library they have either T is the T is the transformer transformer for either it combine it combines a functor with either this is functor and in our case we want functor we want future as a function so this is simple example is a we simulate the external service call for example left is once or service they return the pair value that has something wrong happen and the right we simulate a external service they work well they return the good good value and also we simulate external service they have a future value happen a future ever happen and then this is how we define how we define and when we when we try to catch error and we we can put all this external external code together and then the world because they're all either so they will catch which one fail first and then it will return arrow if all eternal call success they will return the final value final result and we have the results returned and we could get the value value is actually the either so either we can we can handle a good value in the left side and in the right side and bad value in the left side and this is how we handle the future in future video we also have a example to show the simple example this example is we simulate how we handle this first fear like for example if you don't act on service call also good then they will return the good value if anyone feel no matter is the first 107 1 it just is always a fear it's when the failure happened they will stop terminate and return the arrow is catched and also they can catch the future arrow in our project when working out and at the first step way to refactor this error handling the first step when you do is we needed designing the error type we design a general error type and for each action service the type way we define the type for that and the which is ten from the zone general type so that we define each type of action service then in the future when the error happened over now which service has error after that when we were handling a API call this vehicle has a include cause mark electron service how we do that is the first step we need the valid is a request from from the front end request they may have error in the headers they may have error in the body and then if all the and then we also need if our good then we are called external service and after a turn epsilon service will verify the response from excellent service and then wrap up the response return to the front end this is some the call way similar type is defined here for example and then this is our function the API function do what we look like we verify the header the first step we verify the body and we cause the first one API call this one and then we call the second API call week as a result response and they will validated response and return this type is either T then we handle the response and the gallery and the populate the response to the user this we got a value this is over Pisa is a type and we handle the bad case and we return the error 505 and for good case we return okay to the and as a response so in this is this one well we do is we lift who can see here it's here is normally from when we validate the header is just returned the easier but when we stack all this is the T together we want them keep the same type so here we'll lift a either to either T for very data hydro and very body it's saying so after we have that this after weight makes all the type becomes either T then we can easy to they would be they can be composed so we can use for comprehension to stack them together make the logic more clear and if any error happen when each step then they will be stopped if the header has error will be stopped so we return the error so we can find out it's a error in the header or because this is a father this is a fast catch the possible error for all we do the wrap some references here the good one is this heading hat is really good they talk about lots of the cat library how to use that it's very useful and another one is a book from the underscore it's open source so it's also a very good sauce then said the talk okay that's all I have thank you yeah any question okay future dad fail yeah we want to do is we want you we use the future and then either in the return we won't have either but one knows a good value and a better value so one stacking them together so when we do is we can define future and the either in it this one way and actually this one is a hard to put them together like here we try to do is put in for for comprehension so it's hard to stack them together so we did we didn't use that we use either T yes error in the failed feature kiss me [Music] oh yeah we define is correct we define the error in the third trade and so we can it's easy for us to go through those error yes thank you I mean question no okay thank you [Applause]