在 REST 流中调用处理回调时 Cowboy 崩溃
Cowboy crashes when calling handling callback in REST flow
我在使用 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>
...
这些是日志。如您所见,我达到了 "Content negotiation",但没有达到 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 Header?
查看 cowboy_rest
模块代码 here,我认为可能是因为函数 content_types_provided/2
未导出而产生错误。
我在使用 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>
...
这些是日志。如您所见,我达到了 "Content negotiation",但没有达到 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 Header?
查看 cowboy_rest
模块代码 here,我认为可能是因为函数 content_types_provided/2
未导出而产生错误。