【问题标题】:Error in using cowboy in Elixir在 Elixir 中使用牛仔时出错
【发布时间】:2014-04-01 06:26:53
【问题描述】:

我在 Elixir 中使用 Erlang Web 框架 :cowboy,在 :cowboy_http_req.reply 中出现错误,这是我的代码:

mix.exs 是:

defmodule Example.Mixfile do
  use Mix.Project

  def project do
    [ app: :example,
      version: "0.0.1",
      deps: deps ]
  end

  # Configuration for the OTP application
  def application do
    [
      # you should start :inets and :crypto first, or it will not start cowboy
      applications: [:inets, :crypto],
      mod: {Example, []}
    ]
  end

  # Returns the list of dependencies in the format:
  # { :foobar, "0.1", git: "https://github.com/elixir-lang/foobar.git" }
  defp deps do
    [
      { :cowboy, github: "extend/cowboy", tag: "0.9.0" },
      # { :lager, github: "basho/lager" }
    ]
  end
end

lib/example.ex

defmodule 示例执行 使用 Application.Behaviour

  def start(_type, _args) do
    deps_started

    dispatch = :cowboy_router.compile([
      {:_, [
          {"/objects/[...]", Example.Object, []}
      ]}
    ])

    :cowboy.start_http(:http, 100, [ip: {127,0,0,1}, port: 9080], [env: [dispatch: dispatch]])

    ExampleSup.start_link
  end

  defp deps_started do
    deps = [:ranch, :cowlib, :cowboy]
    Enum.all? deps, &ensure_started/1
  end

  defp ensure_started(app) do
    case :application.start(app) do
      :ok ->
        true
      {:error, {:already_started, _app}} ->
        true
      {:error, {:not_started, dep}} ->
        true = ensure_started(dep)
        ensure_started(app)
      error ->
        IO.puts "Couldn't start #{inspect app}: #{inspect error}"
        error
    end
  end
end

而我的 lib/example/object.ex 是:

defmodule Example.Object do
  @behaviour :cowboy_http_handler

  def init(_type, req, _opts) do
    {:ok, req, :undefine}
  end

  def handle(req, state) do
    {ok, req} = :cowboy_http_req.reply(200, [], "hello world", req)
    {:ok, req, state}
  end

  def terminate(_reason, _request, _state), do: :ok
end

我使用 mix 来管理代表,我用idx -S mix 启动牛仔服务器,然后我发送一个 http流使用curl:

curl -i http://127.0.0.1:9080/objects/index.html 

服务器收到错误报告:

=ERROR REPORT==== 28-Feb-2014::18:03:36 ===
Error in process <0.201.0> with exit value: {[{reason,undef},{mfa {'Elixir.Example.Object',handle,2}},{stacktrace,[{cowboy_http_req,reply,[200,[],<<11 bytes>>,{http_req,#Port<0.4438>,ranch_tcp,keepalive,<0.201.0>,<<3 bytes>>,'HTTP/1.1',{{127,0,0,1},58008},<<9 bytes>>,undefined,9080,<<14 bytes>>,[<<5 bytes>>],<<0 bytes>>,undefined,[],[{<<10 bytes>>,<<11 bytes>>},{<<4 bytes>>,<<14 bytes>>},{<<6 bytes>>,<<3 bytes>>}],[],undefined,[],waiting,undefined...

curl 客户端得到了500 Internal Server Error

谁能帮我找出问题所在?非常感谢。

【问题讨论】:

  • 注意:如果您使用的是 Erlang R16B02 或更高版本,则可以使用 application:ensure_all_started(cowboy) 而不是自己递归。
  • 谢谢,我试试看。:)

标签: erlang elixir cowboy


【解决方案1】:

错误信息分为三部分:

  • 原因:undef
  • mfa:{'Elixir.Example.Object',handle,2}
  • 堆栈跟踪:[{cowboy_http_req,reply,[200,[],&lt;&lt;11 bytes&gt;&gt;,...]}, ...]

这是说你在Example.Object 模块中的函数handle/2 中遇到了undef 错误(这意味着未定义的函数)。并且错误具有调用cowboy_http_req.reply/4 的堆栈跟踪。我的预感是 cowboy_http_req 不存在,您应该调用 cowboy_req

我也建议您发送电子邮件至use plug,尽管我们目前还没有可以很好地显示错误的工具,但它即将推出™。我们还将很快发布一个带有 Elixir 的记录器,这应该有助于处理一些消息。

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 2013-09-17
    • 2016-08-02
    • 2017-06-09
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    相关资源
    最近更新 更多