如何在 case 语句或控制器中使用 __STACKTRACE__
How to use __STACKTRACE__ in case statement or controllers
我在 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
我在 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