我在Phoenix应用程序System.stacktrace/0 outside of rescue/catch clauses is deprecated. If you want to support only Elixir v1.7+, you must access __STACKTRACE__ inside a rescue/catch. If you want to support earlier Elixir versions, move System.stacktrace/0 inside a rescue/catch
中遇到过时警告。
事实是,我使用的是Rollbax,如其文档中所述:Rollbax.report(:error, ArgumentError.exception("oops"), System.stacktrace())
,将我正在执行的每个case语句包装在try/rescue块中感觉有点奇怪。例如这一个:
case (SOME_URL |> HTTPoison.get([], [ ssl: [{:versions, [:'tlsv1.2']}] ])) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
Poison.decode!(body, [keys: :atoms])
{:ok, %HTTPoison.Response{status_code: 404}} ->
Rollbax.report(:error, :not_found, System.stacktrace(), %{reason: "Not found"})
{:ok, %HTTPoison.Response{status_code: 503}} ->
{:error, :ehostunreach}
{:error, %HTTPoison.Error{reason: :ehostunreach}} ->
{:error, :ehostunreach}
{:error, %HTTPoison.Error{reason: :timeout}} ->
Rollbax.report(:error, :timeout, System.stacktrace(), %{reason: :timeout})
{:error, %HTTPoison.Error{reason: reason}} ->
Rollbax.report(:error, :unknown, System.stacktrace(), %{reason: reason})
{:error, reason}
end
我不确定如何一次就能获得这些不同的报告。写这份报告的正确方法是什么?
最佳答案
包装整个case
语句:
try do
# ⇓⇓⇓⇓ NOTE BANG
case (SOME_URL |> HTTPoison.get!([], [ ssl: [{:versions, [:'tlsv1.2']}] ])) do
%HTTPoison.Response{status_code: 200, body: body} ->
Poison.decode!(body, [keys: :atoms])
%HTTPoison.Response{status_code: 404} ->
raise HTTPoison.Error, reason: :not_found
%HTTPoison.Response{status_code: 503} ->
{:error, :ehostunreach}
rescue
e in [HTTPoison.Error] ->
case e do
%HTTPoison.Error{reason: :not_found} ->
Rollbax.report(:error, :not_found, __STACKTRACE__, %{reason: "Not found"})
%HTTPoison.Error{reason: :ehostunreach} ->
{:error, :ehostunreach}
%HTTPoison.Error{reason: :timeout} ->
Rollbax.report(:error, :timeout, __STACKTRACE__, %{reason: :timeout})
%HTTPoison.Error{reason: reason} ->
Rollbax.report(:error, :unknown, __STACKTRACE__, %{reason: reason})
{:error, reason}
end
end
关于elixir - 如何在case语句或 Controller 中使用__STACKTRACE__,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55179977/