我当然有一些关于理解递归的事情。我有这个
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
...和
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
假设recursive_function(1234,1)
,我认为递归函数会在query_url/2
返回{:error, :not_found}
后退出,但事实并非如此,递归调用不会退出。
我尝试做的只是向特定网址发出请求,只要返回200
状态就执行一些操作,并在返回非200
状态后退出
答案 0 :(得分:0)
感谢@Dogbert,问题与API提供商有关,无论出于何种原因,他们开始在200
地图中使用ERRORMESSAGE
键返回ERRORARRAY
,此前他们已返回404
}}。
因此将case
替换为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