0

I certainly have some catching up to do on understanding Recursion. I've got this

def query_url(id,page_number) do
 returned_response = HTTPoison.get! "https://some_web_page/#{id}/? pageNumber=#{page_number}"
  case returned_response.status_code do
    200 ->
      {:ok,returned_response.body}
    _ ->
      {:error,:not_found}
  end
end

... and

def recursive_function(id,page_number) do
   case query_url(id,page_number) do
     {:ok,response}  ->
        non_recusive_function(response)
        recursive_function(id,page_number + 1)
    {:error, :not_found} ->
        IO.puts "Exited"
   end
end

Assuming recursive_function(1234,1), It was my thinking that the recursive function would exit once query_url/2 returns {:error, :not_found}, but that's not the case, the recursive call does not exit.

All I'm trying to do is make a get request to a particular url, carry out some actions as long as 200 status was returned and exit once a non 200 status is returned.

5
  • The code looks correct to me. Can you try IO.inspecting every return value to make sure you do get the error case back? Commented May 1, 2017 at 7:34
  • @Dogbert, Issue was with the API provider, for whatever reason, they began returning 200 with an ERROR MESSAGE key where they hitherto returned 404. Thank you! Commented May 1, 2017 at 10:09
  • You might post the resolution you discovered in an answer and mark it as the answer so that others can see the question is now closed. Commented May 1, 2017 at 22:21
  • Highlights something useful to pay attention to, though... the base case should actually exit! (So you might want to put an upper limit on it or something) Commented May 2, 2017 at 2:31
  • Maybe you should answer your own question, I think. Commented May 2, 2017 at 2:31

1 Answer 1

0

Thanks to @Dogbert, Issue was with the API provider, for whatever reason, they began returning 200 with an ERRORMESSAGE key in an ERRORARRAY map where they hitherto returned 404.

So replaced case with a cond

cond do
    returned_response.status_code == 200 and not returned_response["ERRORARRAY"] == []) ->
     {:ok,returned_response.body}
    returned_response.status_code == 400 -> 
     {:error,:not_found}

    true -> 
      {:error,:not_found}
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.