0

在 elixir 中,您可以像这样定义自定义异常:

defmodule AppWeb.CustomError do
  defexception message: "some custom server error", plug_status: 500
end

但这Elixir.Exception不再是

因此,如果您将它与定义了这种类型规范的第三方库一起使用:

  @spec capture_exception(Exception.t, Keyword.t) :: task
  def capture_exception(exception, opts \\ []) do

  ...

  Sentry.capture_exception(AppWeb.CustomError,
                         [stacktrace: System.stacktrace()]

dialyzer崩溃,breaks the contract因为 CustomError 不是异常:

调用 'Elixir.Sentry':capture_exception('Elixir.AppWeb.CustomError',[{'stacktrace',[{atom(),atom(),[any()] | byte(),[{'file', string()} | {'line',pos_integer()}]}]},...]) 违反合约('Elixir.Exception':t(),'Elixir.Keyword':t()) ->任务()

你能以某种方式扩展Elixir.Exception模块AppWeb.CustomError吗?或者如何遵循最佳实践来解决这个问题?

4

1 回答 1

3

您传递的是模块名称 ( AppWeb.CustomError) 而不是它的实例 ( %AppWeb.CustomError{})。模块的实例将满足该Exception.t类型。

Sentry.capture_exception(%AppWeb.CustomError{},
                         [stacktrace: System.stacktrace()]
于 2017-12-11T13:57:52.263 回答