Dynamic Error Creation with Ruby
A useful pattern I've discovered when working on an api or gem for an external webservice is to be able to raise errors dynamically from an api call. Consider the following response to an HTTP request:
Often with external apis, these error codes, types or messages can change rapidly, so you don't want to initialize a long list of these errors in your library. You'd rather create a new error class and let the end user of your library deal with the specific errors.
As a result, you might want to include the following code in your project:
Which has a really simple interface, and can be called within any project fairly easily.
So in a gem that would wrap around this external webservice, you might have the following code:
This certainly makes dealing with the multitude of errors returned by some apis just that much easier.