Tagging Errors in Your Library

14 Jun 2012

Pulled this out of a talk by Avdi. This technique is described at 16:00 in the video.

It's nice for the user of a library to be able to catch any error coming out of the library, but at the same time, it's nice for a library to pass through basic error types like HTTPError. To solve the problem, you can "tag" the HTTPError with your own error class, essentially raising both types of error:

module MyLib::Error; end

def make_a_network_call
rescue HTTPError => e

Update: more in-depth explanation of this technique is available in his book Exceptional Ruby