purescript/purescript-exceptions

Define Class for Convert PS ADT to and from Error

syaiful6 opened this issue · 2 comments

Sometime you need to differentiate Error thrown by your code, and catch only Error you want. The most common use case is when you want to throw Error in Aff, and catch it, but other code might don't want to catch all Error.

Possible Solution

Define a class SomeError or any good name for this purpose, usually these methods will be defined with help of FFI code. But we can provides utility functions here.

class Show e <= SomeError e where
  toError :: e -> Error
  fromError :: Error -> Maybe e
paf31 commented

This seems fragile honestly. I would recommend using ExceptT for this sort of use case.

You could build a combinator for rethrowing exceptions based on a predicate, and you could maybe build your class on top of that. That seems like a decent basis for a separate library.

I'm with @paf31 on this, I think this would be best dealt with in a separate library, at least to start with. Note that you can write code which only catches certain errors already, without needing to define any classes; one example of how to do this is what I did in pulp: https://github.com/purescript-contrib/pulp/blob/b7a9fb4515c229cadc14be59bb969a44817234ef/src/Pulp/System/Files.purs#L27