【发布时间】: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: <type/sub-type> HTTP 标头?
【问题讨论】: