IEx.pry用于调试Argument错误:e​​rlang.binary_to_integer

时间:2016-01-31 15:11:55

标签: elixir phoenix-framework

当我正在玩这里发现的凤凰首发示例https://github.com/ryanswapp/react-phoenix-starter-template时,我遇到了这个错误,我很难调试。

Compiled web/connection.ex
[info] OPTIONS /api/v1/current_user
[info] Sent 204 in 74µs
Compiled web/connection.ex
[info] GET /api/v1/current_user
[info] Sent 500 in 23ms
[error] #PID<0.418.0> running ReactPhoenix.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /api/v1/current_user?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJVc2VyOmMyZDBmZGQ5LWJkNTUtNDJkNS1iMmJlLTAzYjRkNThkOTg0YyIsInBlbSI6e30sImp0aSI6IjAxZGIxNjQ2LTZmZGEtNGI3Yy04YTI1LTRhZWFhYjAzMjE0NiIsImlzcyI6IlJlYWN0UGhvZW5peCIsImlhdCI6MTQ1NDI1MjE4NCwiZXhwIjoxNDU0NTExMzg0LCJhdWQiOiJ0b2tlbiJ9.YiGd-r2g7w7DiAvicq2C0uqhrwM4ZnUXRlnLg53GLag
** (exit) an exception was raised:
    ** (ArgumentError) argument error
        :erlang.binary_to_integer("c2d0fdd9-bd55-42d5-b2be-03b4d58d984c")
        (react_phoenix) lib/react_phoenix/guardian_serializer.ex:10: ReactPhoenix.GuardianSerializer.from_token/1
        lib/guardian/plug/load_resource.ex:27: Guardian.Plug.LoadResource.call/2 
        (react_phoenix) web/router.ex:13: ReactPhoenix.Router.api/2
        (react_phoenix) web/router.ex:1: ReactPhoenix.Router.match/4
        (react_phoenix) web/router.ex:1: ReactPhoenix.Router.do_call/2
        (react_phoenix) lib/react_phoenix/endpoint.ex:1: ReactPhoenix.Endpoint.phoenix_pipeline/1
        (react_phoenix) lib/plug/debugger.ex:93: ReactPhoenix.Endpoint."call (overridable 3)"/2
        (react_phoenix) lib/phoenix/endpoint/render_errors.ex:34: ReactPhoenix.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

错误消息指向的模块如下。我尝试了IEx.pry(如注释掉的代码所示),但即使服务器是通过iex -S mix phoenix.server启动的,它也不会暂停代码进行检查。

defmodule ReactPhoenix.GuardianSerializer do
require IEx
  @behaviour Guardian.Serializer

  alias ReactPhoenix.Repo
  alias ReactPhoenix.User

  def for_token(user = %User{}), do: { :ok, "User:#{user.id}" }
  def for_token(_), do: { :error, "Unknown resource type" }

  def from_token("User:" <> id), do: { :ok, Repo.get(User, String.to_integer(id)) }   # This is line 10

  #def from_token("User:" <> id), do
  #  IEx.pry
  #  IO.inspect id
  #  { :ok, Repo.get(User, String.to_integer(id)) }
  #  end

  def from_token(_), do: { :error, "Unknown resource type" }
end

1 个答案:

答案 0 :(得分:2)

(react_phoenix) lib/react_phoenix/guardian_serializer.ex:10: ReactPhoenix.GuardianSerializer.from_token/1

编辑:我刚才意识到自己很傻,并且这个笑得很开心。实际上,您似乎将字符串(UUID)强制转换为整数,并且这将永远不会起作用,因为它不是字符串的整数表示。评论一下,如果你没有想到它或者让我懈怠,我会帮助你进一步调试,如果需要的话。

此行似乎表示此时您正在使用字符串列表时使用字符列表,反之亦然。在Elixir "字符串被视为二进制文件,而'被视为字符列表。

String.to_char_list

String.to_string

应该能够帮助你并在上面引用的地方进行转换。如果您在该行之前删除IEx.pry,您应该能够进入并测试它。