{"action":"created","issue":{"url":"https://api.github.com/repos/vkhorikov/CSharpFunctionalExtensions/issues/70","repository_url":"https://api.github.com/repos/vkhorikov/CSharpFunctionalExtensions","labels_url":"https://api.github.com/repos/vkhorikov/CSharpFunctionalExtensions/issues/70/labels{/name}","comments_url":"https://api.github.com/repos/vkhorikov/CSharpFunctionalExtensions/issues/70/comments","events_url":"https://api.github.com/repos/vkhorikov/CSharpFunctionalExtensions/issues/70/events","html_url":"https://github.com/vkhorikov/CSharpFunctionalExtensions/issues/70","id":370822206,"node_id":"MDU6SXNzdWUzNzA4MjIyMDY=","number":70,"title":"Result Try method","user":{"login":"kitsu","id":2218853,"node_id":"MDQ6VXNlcjIyMTg4NTM=","avatar_url":"https://avatars1.githubusercontent.com/u/2218853?v=4","gravatar_id":"","url":"https://api.github.com/users/kitsu","html_url":"https://github.com/kitsu","followers_url":"https://api.github.com/users/kitsu/followers","following_url":"https://api.github.com/users/kitsu/following{/other_user}","gists_url":"https://api.github.com/users/kitsu/gists{/gist_id}","starred_url":"https://api.github.com/users/kitsu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kitsu/subscriptions","organizations_url":"https://api.github.com/users/kitsu/orgs","repos_url":"https://api.github.com/users/kitsu/repos","events_url":"https://api.github.com/users/kitsu/events{/privacy}","received_events_url":"https://api.github.com/users/kitsu/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-10-16T22:16:41Z","updated_at":"2018-10-17T11:39:06Z","closed_at":null,"author_association":"NONE","body":"I'm using your Result object with a third-party library that communicates using exceptions. I'd like to _lift_ the exceptions into a failed Result. My current code just returns a different Result from each branch of the try-catch.\r\n\r\nI'm thinking something like:\r\n``` CSharp\r\nResult.Try(() => library.ThingThatMightFail(),\r\n           (ex) => \"Some error message, possibly branching on exception type...\");\r\n\r\n// Where\r\nTry<Result<T>>(Func<T> attempt, Func<Exception,string> failure)\r\n```\r\n\r\nHandling specific exceptions can be done with `ex is SomeException`, and truly unexpected exceptions can be rethrown.\r\n\r\nThis would just be a convenience method, but often the reason I'm using a Result is to hide library exceptions behind more user friendly error messages.\r\n"},"comment":{"url":"https://api.github.com/repos/vkhorikov/CSharpFunctionalExtensions/issues/comments/430594540","html_url":"https://github.com/vkhorikov/CSharpFunctionalExtensions/issues/70#issuecomment-430594540","issue_url":"https://api.github.com/repos/vkhorikov/CSharpFunctionalExtensions/issues/70","id":430594540,"node_id":"MDEyOklzc3VlQ29tbWVudDQzMDU5NDU0MA==","user":{"login":"freakingawesome","id":2660584,"node_id":"MDQ6VXNlcjI2NjA1ODQ=","avatar_url":"https://avatars1.githubusercontent.com/u/2660584?v=4","gravatar_id":"","url":"https://api.github.com/users/freakingawesome","html_url":"https://github.com/freakingawesome","followers_url":"https://api.github.com/users/freakingawesome/followers","following_url":"https://api.github.com/users/freakingawesome/following{/other_user}","gists_url":"https://api.github.com/users/freakingawesome/gists{/gist_id}","starred_url":"https://api.github.com/users/freakingawesome/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/freakingawesome/subscriptions","organizations_url":"https://api.github.com/users/freakingawesome/orgs","repos_url":"https://api.github.com/users/freakingawesome/repos","events_url":"https://api.github.com/users/freakingawesome/events{/privacy}","received_events_url":"https://api.github.com/users/freakingawesome/received_events","type":"User","site_admin":false},"created_at":"2018-10-17T11:39:06Z","updated_at":"2018-10-17T11:39:06Z","author_association":"CONTRIBUTOR","body":"I cobbled together a quick implementation of what this could look like.\r\n\r\n```c#\r\n// static method on Result\r\npublic static Result<T> Try<T>(Func<T> f, Func<Exception, Maybe<string>> handler)\r\n{\r\n    try\r\n    {\r\n        return Result.Ok(f());\r\n    }\r\n    catch (Exception x)\r\n    {\r\n        var handled = handler(x);\r\n        if (handled.HasValue)\r\n        {\r\n            return Result.Fail<T>(handled.Value);\r\n        }\r\n\r\n        throw;\r\n    }\r\n}\r\n\r\n// an extension for chaining\r\npublic static Result<T> OnSuccessTry<T>(this Result<T> self, Func<T> f, Func<Exception, Maybe<string>> handler) =>\r\n    self.IsFailure ? self : Try(f, handler);\r\n```\r\n\r\nIt's important to rethrow via `throw;` rather than `throw x;` because the `throw;` variation retains the original call stack, whereas `throw x;` creates a new call stack.\r\n\r\nThere could be lots of variations on this theme - async, handlers that return `Result`, etc."}}
{"actor":{"display_login":"freakingawesome"}}