How to previous set status code using Ecto

I’m attempting to previous set the status code on my request using a routed plug, but put_status its not setting the :status key, I’m logging conn just before the crash (send_resp in router)

defmodule MyApp.PlugServer.Plugs.CheckRequestFields do
  def init(options), do: options
  alias Plug.Conn

  def call(%Plug.Conn{request_path: path} = conn, opts) do
    {:ok, body_as_json, conn} = Plug.Conn.read_body(conn, opts)
    if path in opts[:paths] do
      verify_tuple = verify_request_fields!(body_as_json, opts[:fields], MyApp.Validate.CustomRules.get_rules(conn.request_path)) ##cuidado com essa linha ao começar muitos roteamentos
  
      conn = case verify_tuple do
        {:ok, _} -> 
          Conn.assign(conn, :resp, verify_tuple)
        {:error, _} -> 
          conn
          |> Conn.assign(:resp, verify_tuple)
          |> Conn.put_status(400)
          |> Plug.Conn.put_resp_content_type("application/json")
      end
    else
      conn
    end
    IO.inspect(conn)
  end



    defp verify_request_fields!(body, fields, rules) do
      {:ok, data} = Jason.decode(body)
        with {:ok, response} <- Validate.validate(data, rules) do
          IO.inspect(response)
            {:ok, response}
        else
          {:error, array_of_errors} ->
            error_map = %{
              "field" => List.first((List.first(array_of_errors).path)),
              "msg" => List.first(array_of_errors).message
            }
                {:error, error_map}
        end
  end
end

##https://thoughtbot.com/blog/testing-elixir-plugs pipelçine  browser
defmodule TestesPay.MyRouter do
  use Plug.Router
  alias MyApp.PlugServer.Plugs.CheckRequestFields
  
  plug CheckRequestFields, fields: ["chain", "coin", "coin", "transaction_value", "transaction_charged", "ref", "ref_fee", "payload"], paths: ["/api/create_contract"]
  plug :match
  plug :dispatch


  get "/api/create_contract" do
    with {:ok, response} <- conn.assigns.resp,
    {:ok, json_response} = Jason.encode(response),
    {:ok, changeset} = MyApp.ControllerCreateContract.create(response)
     do
      conn
      |> put_resp_content_type("application/json")
      |> send_resp(200, json_response)
    else
      {:error, response} ->
        {:ok, json_response} = Jason.encode(response)
        conn
        |> send_resp()
    end
  end

  match _ do
    send_resp(conn, 404, "oops")
  end


end

I’ve had problems using those kind of functions in Ecto and I dont quite get why. I’m using assing to properly send the data to be set as body bcs body append functions wasnt working as well
the output
%Plug.Conn{ adapter: {Plug.Cowboy.Conn, :...}, assigns: %{}, body_params: %Plug.Conn.Unfetched{aspect: :body_params}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "localhost", method: "GET", owner: #PID<0.471.0>, params: %Plug.Conn.Unfetched{aspect: :params}, path_info: ["api", "create_contract"], path_params: %{}, port: 4000, private: %{}, query_params: %Plug.Conn.Unfetched{aspect: :query_params}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [ {"accept", "*/*"}, {"content-length", "151"}, {"content-type", "application/json"}, {"host", "localhost:4000"}, {"user-agent", "insomnia/9.3.1"} ], request_path: "/api/create_contract", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}], scheme: :http, script_name: [], secret_key_base: nil, state: :unset, status: nil }

Looks like you’re missing Plug.Conn — Plug v1.16.1 for the error case in your plug.

oh I see it now, thanks :slight_smile: