由于某种原因,我的 erlang 服务器在每次请求后都会生成一个 case_clause 错误。这是我的代码:
keychain_no_auth_handler.erl
-module(keychain_no_auth_handler).
-export(
[ init/3,
content_types_accepted/2,
terminate/3,
allowed_methods/2,
handle_request/2
]).
-export([is_registered/1]).
init(_Transport, _Req, []) -> {upgrade, protocol, cowboy_rest}.
terminate(_Reason, _Req, _State) -> ok.
allowed_methods(Req, State) -> {[<<"POST">>], Req, State}.
content_types_accepted(Req, State) -> {[{<<"application/json">>, handle_request}], Req, State}.
handle_request(Req, State) ->
{Action, Req2} = cowboy_req:binding(action, Req),
{Reply, Req3} = case Action of
<<"registered">> -> is_registered(Req2)
end,
{ok, Req4} = cowboy_req:reply(200, [{<<"content-type">>, <<"application/json">>}], Reply, Req3),
{ok, Req4, State}
.
is_registered(Req) ->
{Number, Req2} = cowboy_req:qs_val(<<"number">>, Req),
Reply = backend:is_registered(Number),
{Reply, Req2}
.
is_registered [函数]
is_registered(Number) ->
emysql:prepare(is_registered, <<"SELECT `device`.`id` FROM `device` WHERE `device`.`number`= ?">>),
ResultPacket = emysql:execute(auth_pool, is_registered, [Number]),
ResultType = is_record(ResultPacket, result_packet),
case ResultType of
true ->
Field = emysql_util:as_proplist(ResultPacket),
Id = proplists:get_value(<<"device.id">>, Field),
Registered = Id >= 0,
case Registered of
true ->
Reply = {[{response, registered}]},
jiffy:encode(Reply)
;
false ->
Reply = {[{response, available}]},
jiffy:encode(Reply)
end;
false ->
Reply = {[{response, error}]},
jiffy:encode(Reply)
end
.
服务器回复适当的响应,然后在每次请求后生成 case_clause 错误:
=错误报告==== 5-Feb-2014::10:32:39 === 节点“keychain@dnldd”上的进程 <0.182.0> 出错,退出值:{{case_clause,{ok,{http_req ,#Port<0.2286>,ranch_tcp,keepalive,<0.182.0>,<<4 bytes>>,'HTTP/1.1',{{127,0,0,1},49720},<<9 bytes>> ,undefined,4004,<<11 bytes>>,undefined,<<0 bytes>>,[],[{action,<<10 bytes>>}],[{<<4 bytes>>,<<14 bytes >>},{<<10 字节>>,<<10 字节>>},{<<14 字节>>,<<1 字节>>},{<<13 字节>>,<<8 字节>> },{<<6 字节>>,<<51 字节>>},{<<6 字节>>,<<12 字节>>},{<<12 字节>>,<<16 字节>>}, {<<10 字节>>,<<120 字节>>},{<<13 字节>>,<<36 字节>>},{<<6 字节>>,<<3 字节>>},{<<3 字节>>,<<1 字节>>},{<<15 字节>>, <<17 字节>>},{<<15 字节>>,<<14 字节>>}],[{<<12 字节>>,{<<11 字节>>,<<4 字节>>,[ ]}},{<<17 bytes>>,undefined},{<<13 bytes>>,undefined},{<<19 bytes>>,undefined},{<<8 bytes>>,undefined},{< <6 字节>>,[...{<<13 字节>>,未定义},{<<19 字节>>,未定义},{<<8 字节>>,未定义},{<<6 字节>>,[...{<<13 字节>>,未定义},{<<19 字节>>,未定义},{<<8 字节>>,未定义},{<<6 字节>>,[...
=错误报告==== 2014 年 2 月 5 日::10:32:39 === 牧场侦听器 keychain_http 在 <0.182.0> 退出时使用 cowboy_protocol:start_link/4 启动了连接过程,原因是:{{case_clause,{好的,{http_req,#Port<0.2286>,ranch_tcp,keepalive,<0.182.0>,<<"POST">>,'HTTP/1.1',{{127,0,0,1},49720},< <"127.0.0.1">>,undefined,4004,<<"/registered">>,undefined,<<>>,[],[{action,<<"registered">>}],[{<< "host">>,<<"127.0.0.1:4004">>},{<<"connection">>,<<"keep-alive">>},{<<"content-length">>, <<"0">>},{<<"缓存控制">>,<<"无缓存">>},{<<"origin">>,<<"chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop">>},{<<"number">>,<<"233265256262">>},{<<"content-type">>,<<"application/json ">>},{<<"user-agent">>,<<"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36"> >},{<<"邮递员令牌">>,<<"61b7c8f0-b4f8-3bde-8b99-67c141242e5a">>},{<<"接受">>,<<""user-agent">>,<<"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36">>},{<<"postman -token">>,<<"61b7c8f0-b4f8-3bde-8b99-67c141242e5a">>},{<<"接受">>,<<""user-agent">>,<<"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36">>},{<<"postman -token">>,<<"61b7c8f0-b4f8-3bde-8b99-67c141242e5a">>},{<<"接受">>,<<"/ ">>},{<<"dnt">>,<<"1">>},{<<"accept-encoding">>,<<"gzip,deflate,sdch">>},{< <"accept-language">>,<<"en-US,en;q=0.8">>}],[{<<"content-type">>,{<<"application">>,<< "json">>,[]}},{<<"if-modified-since">>,undefined},{<<"if-none-match">>,undefined},{<<"if-unmodified -since">>,undefined},{<<"if-match">>,undefined},{<<"accept">>,[{{<<" ">>,<<" ">>,[ ]},1000,[]}]},{<<"connection">>,[<<"keep-alive">>]}],undefined,[{charset,undefined},{media_type,{<<"text">>,<<"html">>,[]}}],等待,未定义,<<>>,true,done,[],< <>>,undefined},undefined}},[{cowboy_rest,process_content_type,3,[{file,"src/cowboy_rest.erl"},{line,780}]},{cowboy_protocol,execute,4,[{file ,"src/cowboy_protocol.erl"},{line,529}]}]}
我在这里做错了什么?谢谢。