【问题标题】:Cowboy crashes when calling handling callback in REST flow在 REST 流中调用处理回调时,Cowboy 崩溃
【发布时间】:2018-02-09 12:19:29
【问题描述】:

我在使用 Cowboy 2.0(最新 RC)的 Erlang 应用程序中有以下 REST 处理程序。我一直在从头到尾阅读文档,但我无法理解我的代码有什么问题。

init(Req, State) ->
  {cowboy_rest, Req, State}.

content_types_provided(Req, State) ->
  error_logger:info_msg("Content negotiation~n"),
  {[
     {{<<"text">>, <<"html">>, '*'}, my_handler}
   ], Req, State}.

my_handler(Req, State) ->
  error_logger:info_msg("Got here~n"),      
  ...
  <handler logic>
  ...

这些是日志。如您所见,我开始进行“内容协商”,但没有到达 my_handler 回调。

=INFO REPORT==== 31-Aug-2017::13:29:02 ===
Content negotiation

=CRASH REPORT==== 31-Aug-2017::13:29:02 ===
  crasher:
    initial call: cowboy_stream_h:proc_lib_hack/3
    pid: <0.258.0>
    registered_name: []
    exception exit: {{case_clause,no_call},
                     [{cowboy_rest,set_resp_body,2,
                          [{file,
                               "/app/_build/default/lib/cowboy/src/cowboy_rest.erl"},
                           {line,1019}]},
                      {cowboy_rest,upgrade,4,
                          [{file,
                               "/app/_build/default/lib/cowboy/src/cowboy_rest.erl"},
                           {line,238}]},
                      {cowboy_stream_h,execute,3,
                          [{file,
                               "/app/_build/default/lib/cowboy/src/cowboy_stream_h.erl"},
                           {line,179}]},
                      {cowboy_stream_h,proc_lib_hack,3,
                          [{file,
                               "/app/_build/default/lib/cowboy/src/cowboy_stream_h.erl"},
                           {line,164}]},
                      {proc_lib,init_p_do_apply,3,
                          [{file,"proc_lib.erl"},{line,247}]}]}
      in function  cowboy_stream_h:proc_lib_hack/3 (/app/_build/default/lib/cowboy/src/cowboy_stream_h.erl, line 169)
    ancestors: [<0.257.0>,<0.234.0>,<0.233.0>,ranch_sup,<0.222.0>]
    message_queue_len: 0
    messages: []
    links: [<0.257.0>]
    dictionary: []
    trap_exit: false
    status: running
    heap_size: 987
    stack_size: 27
    reductions: 596
  neighbours:

=ERROR REPORT==== 31-Aug-2017::13:29:02 ===
Ranch listener my_http_listener, connection process <0.257.0>, stream 1 had its request process <0.258.0> exit with reason {case_clause,no_call} and stacktrace [{cowboy_rest,set_resp_body,2,[{file,"/app/_build/default/lib/cowboy/src/cowboy_rest.erl"},{line,1019}]},{cowboy_rest,upgrade,4,[{file,"/app/_build/default/lib/cowboy/src/cowboy_rest.erl"},{line,238}]},{cowboy_stream_h,execute,3,[{file,"/app/_build/default/lib/cowboy/src/cowboy_stream_h.erl"},{line,179}]},{cowboy_stream_h,proc_lib_hack,3,[{file,"/app/_build/default/lib/cowboy/src/cowboy_stream_h.erl"},{line,164}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,247}]}]

我不确定content_types_provided/2 回调与所选处理函数的关联是否存在任何问题。我编写了另一个 REST 处理程序,它非常相似并且可以正常工作。

同样作为第二个问题,content_types_provided/2 中是否有办法将每个请求定向到同一个处理程序,而不依赖于请求中提供的 Accept: &lt;type/sub-type&gt; HTTP 标头?

【问题讨论】:

    标签: rest http erlang cowboy


    【解决方案1】:

    通过cowboy_rest模块代码here,我认为有可能产生错误,因为函数content_types_provided/2没有导出。

    【讨论】:

    • 你好@juan.facorro 实际上我已经单独解决了,但我会标记你的问题,因为你非常接近答案。没有被导出的不是content_types_provided/2 函数,而是my_handler/2 函数。来自 Elixir 我习惯于直接根据他们的定义来定义公共和私人乐趣。
    猜你喜欢
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    相关资源
    最近更新 更多