FallbackController for response

My Pheonix app is set up to be a bridge between a legacy JQuery web-app and Hubspot CRM.

I have all the functionality working to capture a contact in the JQuery side, pass it to the Pheonix app, and then sent a Create_Or_Update post to Hubspot leveraging a slightly updated Hubspotex implementation.

When the HTTP POST request is fired to Hubspot, it is returning a 200 response as expected, but I’m throwing an error in my Phoenix app and I’m not sure what to do to avoid the exception. I think I’m not handling the return correctly.

The function sending to Hubspot is:

  def send_to_hubspot(%Person{} = person) do
    email = person.email
    firstname = person.firstName
    lastname = person.lastName
    registration_event = person.event
    mobilephone = person.sms_phone
    body = %{properties: [
                %{property: "firstname", value: firstname},
                %{property: "lastname", value: lastname},
                %{property: "mobilephone", value: mobilephone},
                %{property: "registration_event", value: registration_event}
              ]
            }
    req = Hubspot.Contacts.create_or_update(email, body)
    case Hubspot.request(req) do
      {:ok, %HTTPoison.Response{status_code: 200}} ->
          person
      {:ok, %HTTPoison.Response{status_code: 409}} ->
        IO.puts "Conflict error response if you are trying to update the email address of a record, and there is an existing record with the new email address."
      {:error, %HTTPoison.Error{reason: reason}} ->
        IO.puts reason
    end

  end

I am calling the function after the Ecto/Postgres database is updated in this function (which is where I think my actual problem is…

  def update_person(%Person{} = person, attrs) do
    person
    |> Person.changeset(attrs)
    |> Repo.update()
    send_to_hubspot(person)
  end

And I’m getting the following exception in the console:

[error] #PID<0.538.0> running ScandimensionWeb.Endpoint (connection #PID<0.537.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: PUT /api/people/1
** (exit) an exception was raised:

    ** (FunctionClauseError) no function clause matching in ScandimensionWeb.FallbackController.call/2
        (scandimension) lib/scandimension_web/controllers/fallback_controller.ex:10: 
            ScandimensionWeb.FallbackController.call(%Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, 
                assigns: %{}, 
                before_send: [#Function<0.58261320/1 in Plug.Session.before_send/2>, #Function<1.112466771/1 in Plug.Logger.call/2>, #Function<0.33752287/1 in Phoenix.LiveReloader.before_send_inject_reloader/2>], 
                body_params: %{"person" => %{"email" => "test@test.com", "event" => "AustinMakerFaire", "firstName" => "Tom", "id" => 1, "lastName" => "Tester", "slot_score" => 5, "sms_phone" => "8015551212"}}, 
                cookies: %{"_scandimension_key" => "SFMyNTY.g3QAAAABbQAAAA9jdXJyZW50X3VzZXJfaWRhAQ.n-kVxZ-a6yhdrUfPzwxSbxhtBjqizNzaQF5XPYsXi1g"}, 
                halted: false, host: "localhost", method: "PUT", owner: #PID<0.538.0>, 
                params: %{"id" => "1", "person" => %{"email" => "test@test.com", "event" => "AustinMakerFaire", "firstName" => "Tom", "id" => 1, "lastName" => "Tester", "slot_score" => 5, "sms_phone" => "8015551212"}}, 
                path_info: ["api", "people", "1"], 
                path_params: %{"id" => "1"}, port: 4000, private: %{ScandimensionWeb.Router => {[], %{}}, 
                :phoenix_action => :update, 
                :phoenix_controller => ScandimensionWeb.PersonController, 
                :phoenix_endpoint => ScandimensionWeb.Endpoint, 
                :phoenix_format => "json", 
                :phoenix_layout => {ScandimensionWeb.LayoutView, :app}, 
                :phoenix_pipelines => [:api], 
                :phoenix_router => ScandimensionWeb.Router, 
                :phoenix_view => ScandimensionWeb.PersonView, 
                :plug_session => %{"current_user_id" => 1}, :plug_session_fetch => :done}, 
                query_params: %{}, query_string: "", 
                remote_ip: {127, 0, 0, 1}, r
                eq_cookies: %{"_scandimension_key" => "SFMyNTY.g3QAAAABbQAAAA9jdXJyZW50X3VzZXJfaWRhAQ.n-kVxZ-a6yhdrUfPzwxSbxhtBjqizNzaQF5XPYsXi1g"}, 
                req_headers: [{"accept", "*/*"}, 
                    {"accept-encoding", "gzip, deflate"}, 
                    {"cache-control", "no-cache"}, 
                    {"connection", "keep-alive"}, 
                    {"content-length", "148"}, 
                    {"content-type", "application/json"}, 
                    {"cookie", "_scandimension_key=SFMyNTY.g3QAAAABbQAAAA9jdXJyZW50X3VzZXJfaWRhAQ.n-kVxZ-a6yhdrUfPzwxSbxhtBjqizNzaQF5XPYsXi1g"}, 
                    {"host", "localhost:4000"}, 
                    {"postman-token", "72fffb40-c564-4aa1-91bf-f3c6960ed369"}, 
                    {"user-agent", "PostmanRuntime/7.11.0"}], 
                request_path: "/api/people/1", 
                resp_body: nil, 
                resp_cookies: %{}, 
                resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "FZkgGTd0_LBHp24AAAGC"}], 
                    scheme: :http, 
                    script_name: [], 
                    secret_key_base: :..., 
                    state: :unset, 
                    status: nil}, 
                    %Scandimension.Prospects.Person{__meta__: #Ecto.Schema.Metadata<:loaded, "people">, email: "test@test.com", event: nil, firstName: "Tom", id: 1, inserted_at: ~N[2019-04-25 16:08:38], is_synched: false, lastName: "Tester", slot_score: 5, sms_phone: "8015551212", updated_at: ~N[2019-04-26 20:20:05]})
        (scandimension) lib/scandimension_web/controllers/person_controller.ex:1: ScandimensionWeb.PersonController.action/2
        (scandimension) lib/scandimension_web/controllers/person_controller.ex:1: ScandimensionWeb.PersonController.phoenix_controller_pipeline/2
        (scandimension) lib/scandimension_web/endpoint.ex:1: ScandimensionWeb.Endpoint.instrument/4
        (phoenix) lib/phoenix/router.ex:275: Phoenix.Router.__call__/1
        (scandimension) lib/scandimension_web/endpoint.ex:1: ScandimensionWeb.Endpoint.plug_builder_call/2
        (scandimension) lib/plug/debugger.ex:122: ScandimensionWeb.Endpoint."call (overridable 3)"/2
        (scandimension) lib/scandimension_web/endpoint.ex:1: ScandimensionWeb.Endpoint.call/2
        (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:33: Phoenix.Endpoint.Cowboy2Handler.init/2
        (cowboy) /Users/chipcoons/Dev/Phoenix/scandimension/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
        (cowboy) /Users/chipcoons/Dev/Phoenix/scandimension/deps/cowboy/src/cowboy_stream_h.erl:296: :cowboy_stream_h.execute/3
        (cowboy) /Users/chipcoons/Dev/Phoenix/scandimension/deps/cowboy/src/cowboy_stream_h.erl:274: :cowboy_stream_h.request_process/3
        (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3