我有问题要理解,监护人如何工作或处理错误,所以在这个例子中,我只是尝试注销并在控制台上获得错误..
代码:
def delete(conn, _params) do
{:ok, claims} = Guardian.Plug.claims(conn)
conn
|> Guardian.Plug.current_token
|> Guardian.revoke!(claims)
conn
|> render("delete.json")
end
即使删除了会话,用户也会忘记找不到令牌。 我有调试代码的问题。 如何更好地发现错误或调试代码?
错误消息:
[error] #PID<0.2061.0> running PhoenixExample.Endpoint terminated
Server: 127.0.0.1:4000 (http)
Request: DELETE /api/v1/sessions
** (exit) an exception was raised:
** (Ecto.StaleEntryError) attempted to delete a stale struct:
%GuardianDb.Token{__meta__: #Ecto.Schema.Metadata<:loaded, "guardian_tokens">, aud: "User:96900", claims: %{"aud" => "User:96900", "exp" => 1474754356, "iat" => 1472162356, "iss" => "PhoenixExample", "jti" => "8947def5-ec3e-46e1-866d-2a1e9e16fd1f", "pem" => %{}, "sub" => "User:96900", "typ" => "token"}, exp: 1474754356, inserted_at: #Ecto.DateTime<2016-08-25 21:59:16>, iss: "PhoenixExample", jti: "8947def5-ec3e-46e1-866d-2a1e9e16fd1f", jwt: "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJVc2VyOjk2OTAwIiwiZXhwIjoxNDc0NzU0MzU2LCJpYXQiOjE0NzIxNjIzNTYsImlzcyI6IkNhbGx5YWNvcmUiLCJqdGkiOiI4OTQ3ZGVmNS1lYzNlLTQ2ZTEtODY2ZC0yYTFlOWUxNmZkMWYiLCJwZW0iOnt9LCJzdWIiOiJVc2VyOjk2OTAwIiwidHlwIjoidG9rZW4ifQ.BTYn7wjPlosztNmy3DKeRExzwBOiUG_ZLBJqUzUinDIpEl0xvUnb3EzYM7VaBpIT__JtVfZ0t7XET9D0LK0eMg", sub: "User:96900", typ: "token", updated_at: #Ecto.DateTime<2016-08-25 21:59:16>}
(ecto) lib/ecto/repo/schema.ex:397: Ecto.Repo.Schema.apply/4
(ecto) lib/ecto/repo/schema.ex:341: anonymous fn/9 in Ecto.Repo.Schema.do_delete/4
lib/guardian_db.ex:98: GuardianDb.on_revoke/2
(guardian) lib/guardian.ex:131: Guardian.revoke!/3
(phoenixexample) web/controllers/api/v1/session_controller.ex:30: PhoenixExample.SessionController.delete/2
(phoenixexample) web/controllers/api/v1/session_controller.ex:1: PhoenixExample.SessionController.action/2
(phoenixexample) web/controllers/api/v1/session_controller.ex:1: PhoenixExample.SessionController.phoenix_controller_pipeline/2
(phoenixexample) lib/phoenixexample/endpoint.ex:1: PhoenixExample.Endpoint.instrument/4
(phoenixexample) lib/phoenix/router.ex:261: PhoenixExample.Router.dispatch/2
(phoenixexample) web/router.ex:1: PhoenixExample.Router.do_call/2
(phoenixexample) lib/phoenixexample/endpoint.ex:1: PhoenixExample.Endpoint.phoenix_pipeline/1
(phoenixexample) lib/plug/debugger.ex:122: PhoenixExample.Endpoint."call (overridable 3)"/2
(phoenixexample) lib/phoenixexample/endpoint.ex:1: PhoenixExample.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
conn(已检查)
%Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{},
before_send: [#Function<1.66364769/1 in Plug.Logger.call/2>,
#Function<0.78287744/1 in Phoenix.LiveReloader.before_send_inject_reloader/1>],
body_params: %{}, cookies: %Plug.Conn.Unfetched{aspect: :cookies},
halted: false, host: "127.0.0.1", method: "DELETE", owner: #PID<0.2060.0>,
params: %{}, path_info: ["api", "v1", "sessions"],
peer: {{127, 0, 0, 1}, 63006}, port: 4000,
private: %{PhoenixExample.Router => {[], %{}},
:guardian_default_claims => {:ok,
%{"aud" => "User:96900", "exp" => 1474754356, "iat" => 1472162356,
"iss" => "PhoenixExample", "jti" => "8947def5-ec3e-46e1-866d-2a1e9e16fd1f",
"pem" => %{}, "sub" => "User:96900", "typ" => "token"}},
:guardian_default_jwt => "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJVc2VyOjk2OTAwIiwiZXhwIjoxNDc0NzU0MzU2LCJpYXQiOjE0NzIxNjIzNTYsImlzcyI6IkNhbGx5YWNvcmUiLCJqdGkiOiI4OTQ3ZGVmNS1lYzNlLTQ2ZTEtODY2ZC0yYTFlOWUxNmZkMWYiLCJwZW0iOnt9LCJzdWIiOiJVc2VyOjk2OTAwIiwidHlwIjoidG9rZW4ifQ.BTYn7wjPlosztNmy3DKeRExzwBOiUG_ZLBJqUzUinDIpEl0xvUnb3EzYM7VaBpIT__JtVfZ0t7XET9D0LK0eMg",
:guardian_default_resource => %PhoenixExample.User{__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
account: nil, address: nil, approved: false, city: nil, country: nil,
description: nil, email: "radosch@me.com",
encrypted_password: "$2b$12$E8AOMRI6pVHLIuumenHMSOfCR8TRK4.HYaCSGQvrobgTYkWBdcV7m",
firstname: nil, guid: nil, id: 96900,
inserted_at: #Ecto.DateTime<2016-08-05 01:06:19>, is_provider: false,
lastname: nil, password: nil, postcode: nil, tags: nil,
updated_at: #Ecto.DateTime<2016-08-05 01:06:19>, username: "radosch"},
:phoenix_action => :delete,
:phoenix_controller => PhoenixExample.SessionController,
:phoenix_endpoint => PhoenixExample.Endpoint, :phoenix_format => "json",
:phoenix_layout => {PhoenixExample.LayoutView, :app},
:phoenix_pipelines => [:api],
:phoenix_route => #Function<61.48695856/1 in PhoenixExample.Router.match_route/4>,
:phoenix_router => PhoenixExample.Router,
:phoenix_view => PhoenixExample.SessionView,
:plug_session_fetch => #Function<1.82590416/1 in Plug.Session.fetch_session/1>},
query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1},
req_cookies: %Plug.Conn.Unfetched{aspect: :cookies},
req_headers: [{"host", "127.0.0.1:4000"}, {"content-length", "0"},
{"connection", "keep-alive"}, {"accept", "*/*"},
{"user-agent", "CallYa/1 CFNetwork/808.0.1 Darwin/16.0.0"},
{"accept-language", "de-ch"},
{"authorization",
"Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJVc2VyOjk2OTAwIiwiZXhwIjoxNDc0NzU0MzU2LCJpYXQiOjE0NzIxNjIzNTYsImlzcyI6IkNhbGx5YWNvcmUiLCJqdGkiOiI4OTQ3ZGVmNS1lYzNlLTQ2ZTEtODY2ZC0yYTFlOWUxNmZkMWYiLCJwZW0iOnt9LCJzdWIiOiJVc2VyOjk2OTAwIiwidHlwIjoidG9rZW4ifQ.BTYn7wjPlosztNmy3DKeRExzwBOiUG_ZLBJqUzUinDIpEl0xvUnb3EzYM7VaBpIT__JtVfZ0t7XET9D0LK0eMg"},
{"accept-encoding", "gzip, deflate"}], request_path: "/api/v1/sessions",
resp_body: nil, resp_cookies: %{},
resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"},
{"x-request-id", "bn5l49vfieh6mra9sq4toa9afcocq5c9"}], scheme: :http,
script_name: [],
secret_key_base: "A2LVGKIq88H3nrx6cx4PC7uWf/AAe9zcjwfCRFTx1X5xQ5KNvlLTzfxbGQxQctdb",
state: :unset, status: nil}
%Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{},
before_send: [#Function<1.66364769/1 in Plug.Logger.call/2>,
#Function<0.78287744/1 in Phoenix.LiveReloader.before_send_inject_reloader/1>],
body_params: %{}, cookies: %Plug.Conn.Unfetched{aspect: :cookies},
halted: false, host: "127.0.0.1", method: "DELETE", owner: #PID<0.2061.0>,
params: %{}, path_info: ["api", "v1", "sessions"],
peer: {{127, 0, 0, 1}, 63007}, port: 4000,
private: %{PhoenixExample.Router => {[], %{}},
:guardian_default_claims => {:ok,
%{"aud" => "User:96900", "exp" => 1474754356, "iat" => 1472162356,
"iss" => "PhoenixExample", "jti" => "8947def5-ec3e-46e1-866d-2a1e9e16fd1f",
"pem" => %{}, "sub" => "User:96900", "typ" => "token"}},
:guardian_default_jwt => "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJVc2VyOjk2OTAwIiwiZXhwIjoxNDc0NzU0MzU2LCJpYXQiOjE0NzIxNjIzNTYsImlzcyI6IkNhbGx5YWNvcmUiLCJqdGkiOiI4OTQ3ZGVmNS1lYzNlLTQ2ZTEtODY2ZC0yYTFlOWUxNmZkMWYiLCJwZW0iOnt9LCJzdWIiOiJVc2VyOjk2OTAwIiwidHlwIjoidG9rZW4ifQ.BTYn7wjPlosztNmy3DKeRExzwBOiUG_ZLBJqUzUinDIpEl0xvUnb3EzYM7VaBpIT__JtVfZ0t7XET9D0LK0eMg",
:guardian_default_resource => %PhoenixExample.User{__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
account: nil, address: nil, approved: false, city: nil, country: nil,
description: nil, email: "radosch@me.com",
encrypted_password: "$2b$12$E8AOMRI6pVHLIuumenHMSOfCR8TRK4.HYaCSGQvrobgTYkWBdcV7m",
firstname: nil, guid: nil, id: 96900,
inserted_at: #Ecto.DateTime<2016-08-05 01:06:19>, is_provider: false,
lastname: nil, password: nil, postcode: nil, tags: nil,
updated_at: #Ecto.DateTime<2016-08-05 01:06:19>, username: "radosch"},
:phoenix_action => :delete,
:phoenix_controller => PhoenixExample.SessionController,
:phoenix_endpoint => PhoenixExample.Endpoint, :phoenix_format => "json",
:phoenix_layout => {PhoenixExample.LayoutView, :app},
:phoenix_pipelines => [:api],
:phoenix_route => #Function<61.48695856/1 in PhoenixExample.Router.match_route/4>,
:phoenix_router => PhoenixExample.Router,
:phoenix_view => PhoenixExample.SessionView,
:plug_session_fetch => #Function<1.82590416/1 in Plug.Session.fetch_session/1>},
query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1},
req_cookies: %Plug.Conn.Unfetched{aspect: :cookies},
req_headers: [{"host", "127.0.0.1:4000"}, {"content-length", "0"},
{"connection", "keep-alive"}, {"accept", "*/*"},
{"user-agent", "CallYa/1 CFNetwork/808.0.1 Darwin/16.0.0"},
{"accept-language", "de-ch"},
{"authorization",
"Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJVc2VyOjk2OTAwIiwiZXhwIjoxNDc0NzU0MzU2LCJpYXQiOjE0NzIxNjIzNTYsImlzcyI6IkNhbGx5YWNvcmUiLCJqdGkiOiI4OTQ3ZGVmNS1lYzNlLTQ2ZTEtODY2ZC0yYTFlOWUxNmZkMWYiLCJwZW0iOnt9LCJzdWIiOiJVc2VyOjk2OTAwIiwidHlwIjoidG9rZW4ifQ.BTYn7wjPlosztNmy3DKeRExzwBOiUG_ZLBJqUzUinDIpEl0xvUnb3EzYM7VaBpIT__JtVfZ0t7XET9D0LK0eMg"},
{"accept-encoding", "gzip, deflate"}], request_path: "/api/v1/sessions",
resp_body: nil, resp_cookies: %{},
resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"},
{"x-request-id", "u3c2qbajf1a0fofhlg7hl4aa57gttqrl"}], scheme: :http,
script_name: [],
secret_key_base: "A2LVGKIq88H3nrx6cx4PC7uWf/AAe9zcjwfCRFTx1X5xQ5KNvlLTzfxbGQxQctdb",
state: :unset, status: nil}