Created
June 3, 2018 17:23
-
-
Save AndrewDryga/eb0fd5663a3947349b44a0ba89489d37 to your computer and use it in GitHub Desktop.
Sage: Booking with handling timeout errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you have a small typo in the line
11
Nice article by the way! 👏