Reputation: 421
I have to do some manipulations on some CSV files which are very poorly formatted (several different tables in one file etc).
For this I am using CsvFile.Load and write functions on the Seq of string.
However, to manage exceptions that could arrise from then, I wrote such a function.
let tryWithResult f i =
try
f i |> Result.Ok
with
| e -> ("Exception Raised: " + e.Message + "\n" + e.Source) |> Result.Error
Signature:
val tryWithResult : f:('a -> 'b) -> i:'a -> Result<'b,string>
Do you think it is a proper way to do - in term of F#/FP good practice (and so I can use it within a Railway Oriented Programming https://fsharpforfunandprofit.com/posts/recipe-part2/)?
I know it is always better to avoid exception treatment but I would like something reasonably "safe" but also not spending hours on it.
Thanks
Upvotes: 2
Views: 230
Reputation: 5004
Yes, it is perfectly reasonable to catch all exceptions an make them into Result
s, specially if you are doing a self contained solution.
Letting go of exceptions makes sense if 1) you are designing a library for others to use, or 2) your code is part of a larger framework that already handles well unhandled exceptions and logs the errors appropriately.
Upvotes: 3