Skip to content

Commit 23fb92f

Browse files
author
José Valim
committed
Improve error message for IEx.pry/1
1 parent db61109 commit 23fb92f

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

lib/iex/lib/iex.ex

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -391,25 +391,41 @@ defmodule IEx do
391391
Setting variables or importing modules in IEx does not
392392
affect the caller the environment (hence it is called `pry`).
393393
"""
394-
defmacro pry(timeout \\ 1000) do
394+
defmacro pry(timeout \\ 5000) do
395395
quote do
396-
env = __ENV__
397-
meta = "#{inspect self} at #{Path.relative_to_cwd(env.file)}:#{env.line}"
398-
opts = [binding: binding, dot_iex_path: "", env: env, prefix: "pry"]
399-
res = IEx.Server.take_over("Request to pry #{meta}", opts, unquote(timeout))
400-
401-
# We cannot use colors because IEx may be off.
402-
case res do
403-
{:error, :self} = err ->
404-
IO.puts :stdio, "IEx cannot pry itself."
405-
{:error, :no_iex} = err ->
406-
IO.puts :stdio, "Cannot pry #{meta}. Is an IEx shell running?"
407-
_ ->
408-
:ok
409-
end
396+
IEx.pry(binding, __ENV__, unquote(timeout))
397+
end
398+
end
410399

411-
res
400+
@doc """
401+
Callback for `IEx.pry/1`.
402+
403+
You can invoke this function directly when you are not able to invoke
404+
`IEx.pry/1` as a macro. This function expects the binding (from
405+
`Kernel.binding/0`), the environment (from `__ENV__`) and the timeout
406+
(a sensible default is 5000).
407+
"""
408+
def pry(binding, env, timeout) do
409+
meta = "#{inspect self} at #{Path.relative_to_cwd(env.file)}:#{env.line}"
410+
opts = [binding: binding, dot_iex_path: "", env: env, prefix: "pry"]
411+
res = IEx.Server.take_over("Request to pry #{meta}", opts, timeout)
412+
413+
# We cannot use colors because IEx may be off.
414+
case res do
415+
{:error, :self} ->
416+
IO.puts :stdio, "IEx cannot pry the shell itself."
417+
{:error, :no_iex} ->
418+
extra =
419+
case :os.type do
420+
{:win32, _} -> " If you are Windows, you may need to start IEx with the --werl flag."
421+
_ -> ""
422+
end
423+
IO.puts :stdio, "Cannot pry #{meta}. Is an IEx shell running?" <> extra
424+
_ ->
425+
:ok
412426
end
427+
428+
res
413429
end
414430

415431
## Callbacks

0 commit comments

Comments
 (0)