0
votes

I've got the following REST handler in an Erlang app using Cowboy 2.0 (latest RC). I've been reading the docs front to back and again, and I can't understand what's wrong with my code.

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>
  ...

And these are the logs. As you can see I get up to "Content negotiation", but don't reach the my_handler callback.

=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}]}]

I'm not sure if there's any problem in the association of the content_types_provided/2 callback with the chosen handler function. I've written another REST handler which is very similar and it works correctly.

Also as a second question, is there a way in content_types_provided/2 to direct every request to the same handler, independent of the Accept: <type/sub-type> HTTP Header provided in the request?

1

1 Answers

1
votes

Going through the cowboy_rest module code here, I think it's possible the error is generated because the function content_types_provided/2 is not exported.