Skip to content

Instantly share code, notes, and snippets.

@AndrewDryga
Created June 3, 2018 17:23
Show Gist options
  • Save AndrewDryga/eb0fd5663a3947349b44a0ba89489d37 to your computer and use it in GitHub Desktop.
Save AndrewDryga/eb0fd5663a3947349b44a0ba89489d37 to your computer and use it in GitHub Desktop.
Sage: Booking with handling timeout errors
def book_trip(attrs) do
with {:ok, exchange_rates} <- BillingAPI.fetch_currency_exchange_rates(attrs),
{:ok, card_authorization} <- BillingAPI.authorize_card(exchange_rates, attrs),
{:hotel_booking, {:ok, hotel_booking}, _} <- {:hotel_booking, HotelsBookingAPI.book_hotel(attrs), []},
{:car_booking, {:ok, car_booking}, _} <- {:car_booking, CarsBookingAPI.book_hotel(attrs), [hotel_booking]},
{:flight_booking, {:ok, flight_booking}, _} <- {:flight_booking, FlightsBookingAPI.book_hotel(attrs), [hotel_booking, car_booking]},
:ok <- Mailer.send_email_confirmation(card_authorization, hotel_booking, attrs),
{:charge, {:ok, charge}, _} <- {:charge, BillingAPI.charge_card(card_authorization), [...]} do
{:ok, %{charge: charge, bookings: [hotel_booking, car_booking, flight_booking]}}
else
{:charge, {:error, reason], bookings} ->
:ok = send_excuse_email(authorization)
:ok = cancel_bookings(bookings)
{:error, reason}
{:hotel_booking, {:error, reason}, bookings} ->
:ok = cancel_bookings(bookings)
:ok = HotelsBookingAPI.maybe_ensure_deleted(reason, attrs)
{:error, reason}
{:car_booking, {:error, reason}, bookings} ->
:ok = cancel_bookings(bookings)
:ok = CarsBookingAPI.maybe_ensure_deleted(reason, attrs)
{:error, reason}
# ...
# TODO: 😅 😱 🤬
end
end
@vitorleal
Copy link

I think you have a small typo in the line 11

-    {:charge, {:error, reason], bookings} ->
+    {:charge, {:error, reason}, bookings} ->

Nice article by the way! 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment