问题描述
我有一个端点ERROR: Command errored out with exit status 1:
command: /home/lieu/dev/r3po/sample/.venv/bin/python3 -c 'import sys,setuptools,tokenize; sys.argv[0] = '"'"'/tmp/pip-install-w32z04mo/pyyaml/setup.py'"'"'; __file__='"'"'/tmp/pip-install-w32z04mo/pyyaml/setup.py'"'"';f=getattr(tokenize,'"'"'open'"'"',open)(__file__);code=f.read().replace('"'"'\r\n'"'"','"'"'\n'"'"');f.close();exec(compile(code,__file__,'"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-w32z04mo/pyyaml/pip-egg-info
cwd: /tmp/pip-install-w32z04mo/pyyaml/
Complete output (7 lines):
running egg_info
creating /tmp/pip-install-w32z04mo/pyyaml/pip-egg-info/PyYAML.egg-info
writing /tmp/pip-install-w32z04mo/pyyaml/pip-egg-info/PyYAML.egg-info/PKG-INFO
writing dependency_links to /tmp/pip-install-w32z04mo/pyyaml/pip-egg-info/PyYAML.egg-info/dependency_links.txt
writing top-level names to /tmp/pip-install-w32z04mo/pyyaml/pip-egg-info/PyYAML.egg-info/top_level.txt
writing manifest file '/tmp/pip-install-w32z04mo/pyyaml/pip-egg-info/PyYAML.egg-info/SOURCES.txt'
error: package directory 'lib3/yaml' does not exist
,如果请求包含有效的check
,则端点200
返回auth_token
,否则返回401
。
def check(conn,_) do
if auth_token = get_session(conn,:auth_token) do
send_resp(conn,200,"")
end
send_resp(conn,401,"")
end
在auth_token
为nil
的情况下,这很好用,但是,在定义它的情况下,我收到一个奇怪的错误。
[error] Ranch listener AppWeb.Endpoint.HTTP had connection process started with :cowboy_clear:start_link/4 at #PID<0.6370.0> exit with reason: {:function_clause,[{:cowboy_http,:commands,[{:state,#PID<0.5887.0>,AppWeb.Endpoint.HTTP,#Port<0.1651>,:ranch_tcp,:undefined,%{env: %{dispatch: [{:_,[],[{:_,Phoenix.Endpoint.Cowboy2Handler,{AppWeb.Endpoint,[]}}]}]},stream_handlers: [Plug.Cowboy.Stream]},"",%{},{{127,1},61063},4000},#Reference<0.3022468478.150208513.95764>,true,2,{:ps_request_line,0},65535,1,:done,1000,[{:stream,{Plug.Cowboy.Stream,{:state,#PID<0.6371.0>,:nofin,:normal}},"GET",:"HTTP/1.1",[]}],[{:child,5000,:undefined}]},[{:response,"401 Unauthorized",%{"cache-control" => "max-age=0,private,must-revalidate","content-length" => "0","date" => "Sat,29 Aug 2020 17:32:18 GMT","server" => "Cowboy","x-request-id" => "Fi_Nf1nWhRxOi1UAAQPB"},""}]],[file: '/Users/raph/Code/App/api/deps/cowboy/src/cowboy_http.erl',line: 954]},{:cowboy_http,:loop,line: 254]},{:proc_lib,:init_p_do_apply,3,[file: 'proc_lib.erl',line: 226]}]}
注意:在所有情况下都成功发送了响应,并且随后出现了以上错误。
错误是什么意思,我该如何避免?
更新
我刚刚注意到上面的代码似乎试图发送两个响应:
[info] Sent 200 in 41ms
[info] Sent 401 in 41ms
鉴于我的真实代码更接近:
def check(conn,:auth_token) do
if user = Accounts.get_user_by_session_token(auth_token) do
send_resp(conn,Map.get(user,:email))
end
end
send_resp(conn,"")
end
编写上述逻辑以使send_resp
仅被调用一次的惯用方式是什么?
下面的实现可以正常工作,但是感觉可以以一种更加惯用的方式进行重构。
def check(conn,:email))
else
send_resp(conn,"")
end
else
send_resp(conn,"")
end
end
解决方法
假设有一个User
结构,我认为这有点惯用了:
def check(conn,_) do
with token when is_binary(token) <- get_session(conn,:auth_token),%User{} = user <- Accounts.get_user_by_session_token(token) do
send_resp(conn,200,user.email)
else
_ -> send_resp(conn,401,"")
end
end