Suppose I want to wrap bs-fetch (in a React hook). Suppose I want the result to peruse Result.t, like Relude.AsyncResult does. Suppose that then I want to use decco for decoding (it speaks in Result, nice).
Suppose also that I’ve read Composable Error Handling in OCaml and want to use polymorphic variants for errors. Here are the issues I’ve met so far:
-
bs-fetch raises exceptions. Catching them is not hard, but distinguishing individual errors by messages is a little too fragile, so the best I can do is just
[`FetchError(string)], right? And error messages coming from libs are not always good to show to users: they’re not localizable, they can contain sensitive info, and they couple presentation to the innards. - Decco just uses records for errors. Those can be mapped to polymorphic variants, I guess, but it looks like it’s going to be one variant as well, e.g.
[`DecodingError(Decco.decodeError)].
So at this point, it’s just 2 variants. Maybe 3, if I add a validation step:
[>
`FetchError(string),
`DecodingError(Decco.decodeError),
`ValidationError(validationError),
]
Questions:
- Do you guys think those variants are granular enough?
- Is it even worth it like that? )
- Can we expect more libraries supporting this pattern?
(I know, I know, answers like these would probably be closed over at StackOverflow
)
. But I’ll look into it once I have time.
.