Recommended way to handle errors


I have a suspicion Js.Json must be semi-deprecated (bs-json is probably the way to go), but I tried to decode JSON the way it’s done in an example there and now I have questions regarding errors in general.

  1. Should we use failwith at all? Seems like some Caml pervasive, and I’ve got no idea how to catch it, so I’ve ended up using Js.Exn.raiseTypeError anyway.

  2. Isn’t Belt.Result meant for error handling? Of course it would mean flatMaps everywhere so maybe that’s not the recommended style, but as a user, I’d like to know the recommended style.


I recommend looking at bs-decode for decoding JSON. It provides a type-safe API for handling errors without throwing exceptions.


Interesting, thanks!

My question was rather about the recommended way to handle errors in general, but the existence of tools like this confirms my doubts about exceptions.

Then again, if ReasonReact adopts React Suspense, we’re gonna see a lot of exceptions anyway :slight_smile:.


This article has a good explanation of error handling in OCaml/ReasonML.


Hey, that’s a great article, thanks!

I still wonder what’s if there’s a recommended approach. Somehow I don’t see Let_syntax in Belt.Result :slight_smile:.


Also there’s a relevant Reason town


Let_syntax is in core. you probably need something similiar for let_anything


Yeah, you’re right.


You can monadically compose the errors without ppx_let, I’ve been doing that on BuckleScript projects without any problem.


How? With infix operators? Cheng Lou says they‘d like to avoid them in Belt, so I guess as a good citizen, I’d like to try and avoid them too.


I’m using infix operators, and it’s working great. I can’t imagine writing the long pipelines without them. I miss ppx_let though, but I’m hoping I can use it in BS projects soon.


Is Let_anything inferior to pxx_let?