Phoenix日志中出现奇怪错误:运行MyApp.Endpoint已终止

Sau*_*abh 14 elixir web phoenix-framework

我在凤凰 webapp中遇到错误,我正在开发.这是一个包含少量模型和页面的简单应用程序,其中所有模型创建表单都以多标签形式添加.

我在日志中遇到以下错误,但是在浏览器结束时工作正常.从日志中我也无法理解错误以及如何调试它.

[error] #PID<0.1603.0> running MyApp.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /
** (exit) an exception was raised:
    ** (Plug.Conn.AlreadySentError) the response was already sent
        (plug) lib/plug/conn.ex:428: Plug.Conn.resp/3
        (plug) lib/plug/conn.ex:415: Plug.Conn.send_resp/3
        (my_app) web/controllers/personal_info_controller.ex:1: MyApp.PersonalInfoController.phoenix_controller_pipeline/2
        (my_app) lib/phoenix/router.ex:265: MyApp.Router.dispatch/2
        (my_app) web/router.ex:1: MyApp.Router.do_call/2
        (my_app) lib/my_app/endpoint.ex:1: MyApp.Endpoint.phoenix_pipeline/1
        (my_app) lib/plug/debugger.ex:90: MyApp.Endpoint."call (overridable 3)"/2
        (my_app) lib/phoenix/endpoint/render_errors.ex:34: MyApp.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
Run Code Online (Sandbox Code Playgroud)

Gaz*_*ler 19

复制响应时会出现此错误 - 当您有一个插件会在某些错误上发送响应(例如,用户未登录),然后从控制器操作发送另一个响应时,通常会发生这种情况.

如果没有看到您的代码,很难告诉您问题出在哪里.发送响应后,您可以使用Plug.Conn.halt/1来防止此错误.

例如

conn
|> send_resp(404, "Post not found")
|> halt
Run Code Online (Sandbox Code Playgroud)


nei*_*ond 10

plug :action从控制器中删除为我修复它.根据https://github.com/phoenixframework/phoenix/issues/888不再需要它

Phoenix/Elixir/Ejabberd建议使用此解决方案- 响应已发送错误