Wrapped exceptions in Ruby

Sometimes you want to raise an exception when a method fails but without losing information about an inner exception. Let me explain it with an example.

At Watu we have a method that causes a user to be indexed in our search engine. This method is called many times in different situations. One of those situations is when indexing most or all of our users. Recently, something failed and I got this exception:

undefined method `each' for #<String:0x10dab8fc>

Something that should be an array is actually a string. In this case the indexing method is correct, it’s just getting broken data. I want to fix the bug in the data generation, but to locate it, I need to know which user has broken data. I added a rescue clause to my indexing method to show me that data:

def index
  # ...
  raise "Error when indexing user #{self}"

Now I get something like:

Error when indexing user #<User:1234>

which allows me know that user 1234 has something wrong in its data. The problem is that now I have no idea what the issue is. I lost the information about trying to call each on a string.

The solution to this problem is exception wrapping (or nesting). You want the custom exception to wrap the other one so that you have both pieces of information. This, for example, exists in Java and if you search the web you’ll find ways on how to implement it in Ruby. Implementing this manually is not needed anymore since Ruby 2.1. Unfortunately, it’s a bit hidden and the tools haven’t caught up yet.

The secret lies in a new method in the class Exception called, cause. At the time of this writing it doesn’t even have documentation:

No documentation for the method Exception#cause

No documentation for the method Exception#cause

Using it is very straightforward. Just raise an exception in the rescue clause and the new exception will have the previous one as its cause. For example, in this case:

  a = 1 / 0
  raise "Something went wrong"

you get two exceptions: the divided-by-zero wrapped inside one with the “Something went wrong” message.

The problem arrises that nobody seems to be using the causes of exceptions yet. If you run that in IRB, this is what you get:

RuntimeError: Something went wrong
        from (irb):4:in `rescue in irb_binding'
        from (irb):1
        from /Users/pupeno/.rvm/rubies/ruby-2.1.0/bin/irb:11:in `&lt;main&gt;'

But the exception’s cause is in there… hidden. If you catch the outer exception you can access its cause. For example:

    a = 1 / 0
    raise "Something went wrong"
rescue => e
  puts e
  puts e.cause

would produce:

Something went wrong
divided by 0

The reason why it doesn’t produce something like that by default is because whatever IRB is using to print exceptions is ignoring the exception’s cause. Now we’ll have to wait until all the tools catch up with this new feature.

Well, we don’t actually have to wait. Aside from the fact that most of them are open source and you can fix them yourself, Ruby allows you to monkey patch so you can fix your own copy of these tools.

In my case I needed rake to print inner exceptions, so I wrote this monkey patch (which works for rake 10.1.1):

module Rake
  class Application
    def display_error_message(ex)
      trace "#{name} aborted!"
      trace "Tasks: #{ex.chain}" if has_chain?(ex)
      trace "(See full trace by running task with --trace)" unless options.backtrace


    def display_exception(ex, margin="")
      trace "#{margin}#{ex.message}"
      if options.backtrace
        trace "#{margin}#{ex.backtrace.join("\n#{margin}")}"
        trace "#{margin}#{Backtrace.collapse(ex.backtrace).join("\n#{margin}")}"
      if ex.respond_to?(:cause) && !ex.cause.nil? # Ruby < 2.1.0 doesn't have *cause*
        trace "#{margin}which was caused by:"
        display_exception(ex.cause, "#{margin} ")

This is something that I would like to see in rake itself so I created an issue request (#253). Take a look at it to follow the development of this feature and hopefully, all tools will start displaying causes in one way or another.

The danger of the wide product

Working at Watu I came up with the categorization of products by width. There are products that are wide and products that are narrow. They have different traits and understanding those traits is important. Watu is a wide product. Twitter is a narrow product while Facebook is wider. This is not a matter of complexity or size but a matter of how many modules or independent parts a product has. An example of a narrow but complex product is Apple’s Siri.

GitHub used to be a narrow product: git repositories. Now it is a wider product: git repositories plus issue tracker, plus wiki for documentation, plus public pages. You can think of more features that GitHub could add to make it a wider product: product management, customer management, etc. Adding those features make GitHub a wider product, while adding pull requests handling doesn’t.

The advantage of wider products is that they are the all in one solution for more people than narrow products. That’s because people tend to have a wide variety of needs. If your social needs is just sending short messages, Twitter is the all-in-one, but if you also share pics, organize events, form groups, etc., then Twitter is no longer the all-in-one product and you need a wider solution, like Facebook.

The advantage of being the all-in-one product is that if your users are not looking outside your product they are less likely to jump to the competition. They are also more likely to put up with an inferior solution in one or several aspects because the other aspects make up for it and the seamless interconnection of the different parts of the product is in itself a big plus.

For example, if Facebook implements a Doodle-like module, it doesn’t have to be as good as Doodle to make me switch to it, because I’m already inside Facebook for socializing and event handling, so also using it for deciding when an event happens is very convenient (Facebook, please, don’t kill Doodle… just buy them if you have to).

But, there are some dangers to building wide products. Once is that it’s harder to keep focus because you need to constantly jump between modules. If I was to develop Twitter by myself I would be much more effective than if I was to develop Facebook, because I would have less context switching. I believe this point is not true when you have more people than modules so each person or team can keep focus. But when you are a small three-person startup, this is something worth considering.

Another danger of having a wide product is that, even as a developer, it’s scary to jump outside. At Watu we saw several opportunities to build different products where a customer came with a need that didn’t match Watu perfectly. Every time we discarded it because building a new product and making it as wide as Watu was too much work and modifying Watu was undesirable. The truth is that maybe those products didn’t need to be as wide, they didn’t need all these modules and features, but that fact was very hard to see when we were living and breathing Watu every day.

A return to space

In the 1990s we had what I consider the two best space related TV shows ever produced: Star Trek: The Next Generation and Babylon 5. TNG showed humans as explorers, evolved beings trying to move forward peacefully. It was a beautiful picture, something to aim for. Babylon 5 was more realistic: we still have problems and it’ll be a struggle, but we can do it. Babylon 5: In the beginning, one of the movies of the franchise, has some of the most realistic space battles. I read they got NASA’s Jet Propulsion Laboratory to do the math for them.

And then space died.

It was a slow and painful death. The spinoff of B5 lasted half a season. Star Trek managed to go on for much longer, but the quality went down until Enterprise was cancelled before a proper ending. And then we have Firefly… possibly the last big attempt at depicting humans living in space. It suffered the same fate as B5’s spinoff.

These developments are not just about fiction. Space exploration has been disappointing us for the last 30 years or so. We never went back to the moon. We never went to Mars. The International Space Station is amazing, but it’s not the space habitats we used to dream about. NASA cancelled the Space Shuttle program. The Hubble telescope was almost decommissioned. We stopped dreaming…

Well, not everybody stopped dreaming. And in a world where making a difference is becoming easier every day, some people have enough resources to do it even when it comes to something as expensive and crazy as space. People like Jeff Bezos (Blue Origin), Richard Branson (Virgin Galactic), and my favorite, Elon Musk (SpaceX):

In 2009 we saw the return of Star Trek with J. J. Abrams reboot. It’s not a good Star Trek movie but it is a good action movie, that happens in space, in the future, a future where humans have expanded throughout the galaxy and created a peaceful federation of planets. And this year we had another Star Trek movie for which I would say the previous statement applies as well. They are fun… but what really excites me is this:

potentially realistic depictions of space…

Maybe we are looking up again… hopefully… we’ll start dreaming again soon.

I need a new word

Yesterday I was going to meet a friend. I arrived very late due to poor planning on my side (leaving late) but also because I got lost. Well… I didn’t really get lost. I knew exactly where I was all the time. Pinpoint accuracy by my GPS. But I didn’t know how to proceed because buses got diverted, some may have been cancelled but I wasn’t sure.

One part of the definition of “lost” is the correct one:

Unable to find one’s way

but the other part is not:

not knowing one’s whereabouts

and when I say I got lost, I may sound like a lier: “How can you possible get lost with your phone/GPS/google maps?”. I need a new word that means “I know where I am but not how to proceed.”

I invented space elevators

I read The Last Theorem by Arthur C. Clarke and Frederik Pohl and an important part of the plot is the construction of a space elevator. There, the authors explain to you what an space elevator is, how does it work, what are the challenges and even who invented them. Apparently in the 19th century some guys already dreamed (and wrote about) space elevators. I bet it was dreamed even before that, but at any rate, I invented them as well.

Picture of scaffoldingWhen I was a kid, the house where I lived used to have scaffoldings here and there quite frequently. It was never finished. I grew up playing in scaffoldings and like some kids build a house in a tree, I built it on scaffolding. It was amazing! I loved it. Obviously I started thinking how high could I build a scaffold? With my child’s mind I saw no limit but I realized that at some point, the outer part would pull instead of push and I fantasized about a scaffold that would reach the moon. A bit more than a mere space elevator.

In my phantasy one would climb the scaffold in a space suit, reach the top and when the moon passed by grab onto it: a small grab for a mind, a giant leap for the scaffolding industry.

Total Recalls

We did the crazy thing. We watched 1990’s Total Recall while eating burritos and then went to the cinema to watch the remake. It’s hard to figure out which one is worse. Which is quite an achievement if you think about it. But let me start with a positive note, before I go all ranty about a single aspect of the new version.

Our beloved protagonist… bah… who am I kidding… Quaid is in a quest to get his memories back and eventually finds someone who can help. In the old movie, it’s a super-weird psychic known as Kuato that by magic manages to restore some memories. In case you don’t remember:

In the new one, Quaid is instead connected to a computer through electrodes in his head. They replaced psychics with technology. I can’t help but believe that it’s a small piece of evidence that humanity is growing up, stopping to believe in fairy tales and trusting its fate in technology and engineering. At last, three or four hundred years of steady progress in improving people’s quality of life are paying of (mind you, also fueling a few terrible wars too).

Now to the rant! This was such a blatant disregard of the laws of nature that I felt like walking away from the movie, like I did with Lockout.

Let’s start with some facts. What happens if you put people inside a vessel, like a car or an elevator, and drop it? Let me show you what happens by showing you a scene from the movie that made it to the trailer (jump to 2:00):

Disregard the part when they decelerate in only three meters and still survive.

What happens is that the contents of the vessel seem to float because they are free falling with the same acceleration and initial speed as the vessel. In a more complex scenario but still using the same principles, you can experience weightlessness inside an aircraft. NASA uses that to train astronauts, Hollywood used that to film Apollo 13 (jump to 4:15):

And if you seen the movie you know where I’m going with this. In the new version, there’s a hole through the earth, from the UK to Australia. I’m not going to analyze how expensive that is or why it makes no sense. Keeping all our houses at spring temperature all year around, no matter whether it’s hotter or colder outside was, at some point, so impossible people wouldn’t even dream of it (specially the cooling down part). Science fiction is about dreaming, so, let’s dream about a hole from UK to Australia.

This hole is used for transport. They have a huge cylinder that people enter that travels through the hole. They sit in rows and get strapped to the seats like in a roller-coaster. I’m not spoiling anything, this is shown at the very beginning. They drop the cylinder and… what should happen now? Think of the car being dropped and make your guess: The cylinder is in free fall and so is everything inside it, things should appear to float… that’s not what happens in the movie.

In the movie, they enjoy gravity… and if you think that’s bad… get ready for more. When they are getting closer to the center of the earth, the PA system says something like “get ready for gravity reversal”. For a period of time they are weightless and some parts of the cylinder, with people strapped to the seats, is turned around, which is quite cool. When they leave the core of the planet, gravity comes back like someone waking up from a nightmare and everything falls. That is so wrong.

There seems to be no extra propulsion system, which if the hole through the earth is not vacum, would make for a very long trip… 20 hours or so? I don’t know, I didn’t calculate it. Even if the hole is free of air it’ll take a while… 2 hours? 3 hours? I don’t know… again, I didn’t do the math, if you want it, just ask me and I’ll do it.

What is more important though, is that without any propulsion system, you wouldn’t get to the other side of the earth. If you did, you’d have a perpetual motion machine, which violates the second law of thermodinaics, rigorously formulated by Sadi Carnot in 1824. Let’s assume there’s a propulsion system, ok? An invisible one… let’s say… it’s maglev on the walls or something like that… please? thank you.

Now Hollywood says: but isn’t this whole gravity reversal extremely cool? we want it on the big screen!

Agreed… but Mr Hollywood, hear me out… this is how that transport system should have worked:

  1. Everybody goes inside the huge vessel and get themselves strapped to the seats like in a roller-coaster… it looks cool and realistic, as it’ll be quite a ride.
  2. We have a countdown (awesome! why don’t airplanes have countdowns? imagine everybody going in unison 5, 4, 3, 2, 1, brakes released! take off!).
  3. The cylinder drops and everybody inside experience instant zero gravity… things float, drinks are spilled, people vomit… ok… not that one.
  4. We turn everybody upside down, like they did in the movie, while they are weightless.
  5. We hit the rockets… yes… the vessel now has rockets to accelerate. Or maglev, or whatever, doesn’t matter. The vessel accelerates at 2g (1g is gravity’s pull, another g is caused by the rockets), making earth-like artificial gravity. Alternative the vessel accelerates at 3g or 4g, because they are just a bunch of shitty workers and can take it. If that acceleration is kept, by the time it reaches the core, it’d be traveling awfully fast, and by that I think space-shuttle-worthy fast… I can do the math if you ask me.
  6. At some point they turn the rockets off and it starts falling at 1g again and everybody inside experiences another weightless moment.
  7. We turn everybody around again.
  8. We fire the rockets in the oposite direction… remember, we need as much force to stop it as we need it to get it moving, so everybody experiences gravity again. Instead of rockets, we could just use the atmosphere inside the tube to slow it down. Air is used to slow down spaceships coming back to Earth all the time, and that’s why landing on Mars is harder than landing on Earth… you have less atmosphere to slow you down.
  9. Getting closer to the end of the trip, the strong deceleration stops and everybody experiences zero gravity once again, which is quite convenient, because we turned them upside down twice, which leaves them pointing up in the UK, but pointing down in Australia. So why they experience zero g again, we turn them again and we slowly stop it just in time to see some kangaroos.

So… Hollywood… wouldn’t that have been much more impressive? you had rockets and instead of one wrong gravity reversal, you’d have three correct ones in every trip. Think of the plot potential! I’m giving you three for the price of one!

I’m 30 years old

We evolved ten fingers, so there seems to be something specially about how old I am today: 30. Even though I consider 32 or 64 a more round number, it’s impossible not to do some retrospective. Also… because when I was 25 years old, freshly hired by Google, I set myself the goal of being a millionaire by 30. That… didn’t happen. Nevertheless I’m really happy about where I am today and where I’m going.

The matching t-shirt was just a coincidence

My 30s start with me having a job I love, at last, product-building. I’m the CTO of my own startup, Watu. I’m surrounded with extremely capable and smart people.

For the first time in my life I’m living in a city I truly chosen: London. Until now, every city I lived in (hint: many) was a side effect of a good job in that place, or someone else’s decision that I accepted. This time I picked (ironically, the best job of my life followed) and I love it. London is a wonderful place. There’s always stuff to do, there’s so much variety and you can find almost everything.

My 30s also find me weighting 75kg. My impossible dream was to reach 74kg. It no longer seems impossible (I actually touched 74.3kg last week). But that’s not all, I’m also the fittest I ever been, at least as an adult. In the last couple of years I started running, and managed to run 6.25km. Then I jumped into water and I made a splash by breaking my almost 15 year old personal best. I’m already planning to break it again and go for 5km.

On top of that I’m surrounding myself with interesting and fascinating people that I like spending time with. This is probably my biggest challenge ever.

My life is still not yet all that what I want, but I’m getting there.