This seems pretty fragile to me, but I've no idea what node's best-practices are for this sort of error handling.