Hacker News new | ask | show | jobs
by fennecfoxen 1720 days ago
I feel a lot of the problem here is that someone make the FooLibrary that does some file system call and sometimes throws some IOError. Later on a new backend goes over the network and it throws a NetworkError now too.

This is a leaked abstraction. These are both a kind of FooError. If you’re using a strong enough type system that cares about these things, you should be using it to invest in that sort of strong encapsulation, too. Each API layer should be a translation layer.

Needless to say, this is a fair bit of work, and takes a fair amount of rigor, so it usually won’t happen.

1 comments

I 95% agree with you. But I think it's just slightly more nuanced. If you tell FooLibrary to open a FooFile, then throwing FileNotFoundException is acceptable to signal that that file is not found. In a sense it's just passing along an exception that was meant for you.

On the other hand, if FooLibrary tries to read some config file or fetch metadata over the internet, then that's a sort of detail that it shouldn't bother the caller with. It should either handle the exception or translate it to a FooException.