0
我在使用Cowboy 2.0(最新的RC)的Erlang应用程序中获得了以下REST处理程序。我一直在阅读文档前面,并且我不明白我的代码有什么问题。在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: <type/sub-type>
HTTP标头?
Hello @ juan.facorro其实我已经解决了它,但我会标记你的问题,因为你非常接近答案。它不是''content_types_provided/2'函数不是被导出,而是'my_handler/2'函数。来自Elixir我习惯于直接定义公共和私人乐队。 –