Get stacktrace from inside Code.eval_quoted












0















Is it possible to get a full stacktrace or see on which line an error gets throw when evaluating a quoted block in Elixir?



For example I have this module:



defmodule Test do
def trySomeQuotedCode() do
quote do
IO.puts "line 1"
IO.puts "line 2"
5/0
end
|> evalMyQuoted
end

def evalMyQuoted(quoted) do
Code.eval_quoted(quoted)
end
end


But if you execute it, you see this:



stack trace until 'quoted' call



It shows there was an ArithmeticError with :erlang./(5, 0), which is correct but it does not show where in the quoted code. With this small example it's still easy to find where the error lies in the code, but if this quoted code is a lot bigger or more advanced it may not be so trivial.



So, for this example, is it possible to get the stacktrace to say that error is on "line 3" inside the evaluation of the quoted part? Or perhaps get the line number as a return value from Code.eval_quoted?










share|improve this question





























    0















    Is it possible to get a full stacktrace or see on which line an error gets throw when evaluating a quoted block in Elixir?



    For example I have this module:



    defmodule Test do
    def trySomeQuotedCode() do
    quote do
    IO.puts "line 1"
    IO.puts "line 2"
    5/0
    end
    |> evalMyQuoted
    end

    def evalMyQuoted(quoted) do
    Code.eval_quoted(quoted)
    end
    end


    But if you execute it, you see this:



    stack trace until 'quoted' call



    It shows there was an ArithmeticError with :erlang./(5, 0), which is correct but it does not show where in the quoted code. With this small example it's still easy to find where the error lies in the code, but if this quoted code is a lot bigger or more advanced it may not be so trivial.



    So, for this example, is it possible to get the stacktrace to say that error is on "line 3" inside the evaluation of the quoted part? Or perhaps get the line number as a return value from Code.eval_quoted?










    share|improve this question



























      0












      0








      0








      Is it possible to get a full stacktrace or see on which line an error gets throw when evaluating a quoted block in Elixir?



      For example I have this module:



      defmodule Test do
      def trySomeQuotedCode() do
      quote do
      IO.puts "line 1"
      IO.puts "line 2"
      5/0
      end
      |> evalMyQuoted
      end

      def evalMyQuoted(quoted) do
      Code.eval_quoted(quoted)
      end
      end


      But if you execute it, you see this:



      stack trace until 'quoted' call



      It shows there was an ArithmeticError with :erlang./(5, 0), which is correct but it does not show where in the quoted code. With this small example it's still easy to find where the error lies in the code, but if this quoted code is a lot bigger or more advanced it may not be so trivial.



      So, for this example, is it possible to get the stacktrace to say that error is on "line 3" inside the evaluation of the quoted part? Or perhaps get the line number as a return value from Code.eval_quoted?










      share|improve this question
















      Is it possible to get a full stacktrace or see on which line an error gets throw when evaluating a quoted block in Elixir?



      For example I have this module:



      defmodule Test do
      def trySomeQuotedCode() do
      quote do
      IO.puts "line 1"
      IO.puts "line 2"
      5/0
      end
      |> evalMyQuoted
      end

      def evalMyQuoted(quoted) do
      Code.eval_quoted(quoted)
      end
      end


      But if you execute it, you see this:



      stack trace until 'quoted' call



      It shows there was an ArithmeticError with :erlang./(5, 0), which is correct but it does not show where in the quoted code. With this small example it's still easy to find where the error lies in the code, but if this quoted code is a lot bigger or more advanced it may not be so trivial.



      So, for this example, is it possible to get the stacktrace to say that error is on "line 3" inside the evaluation of the quoted part? Or perhaps get the line number as a return value from Code.eval_quoted?







      elixir stack-trace






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 5:10









      Sheharyar

      45.6k10109162




      45.6k10109162










      asked Nov 24 '18 at 16:50









      Joachim Alvarez-RodriguezJoachim Alvarez-Rodriguez

      31




      31
























          2 Answers
          2






          active

          oldest

          votes


















          0














          TLDR: Use location: :keep.





          You can define your quote inside a macro, like this:



          defmodule Test do
          defmacro my_quote do
          quote do
          IO.puts "line 1"
          IO.puts "line 2"
          5/0
          end
          end

          def try_quote do
          my_quote() # Line 11
          end
          end


          Now calling Test.try_quote:



          iex(7)> Test.try_quote
          line 1
          line 2
          ** (ArithmeticError) bad argument in arithmetic expression
          test.exs:11: Test.try_quote/0


          So we get line where the quote was called, which is better, but not yet what we wanted.



          A solution then could be to use the macro to define a function for us, like this:



          defmodule TestMacro do
          defmacro my_quote do
          quote do
          def my_function do
          IO.puts "line 1"
          IO.puts "line 2"
          5/0 # Line 7
          end
          end
          end
          end

          defmodule Test do
          import TestMacro
          my_quote # Line 15
          end


          Now calling Test.my_function gives us:



          line 1
          line 2
          ** (ArithmeticError) bad argument in arithmetic expression
          test.exs:15: Test.my_function/0


          Which is still the line where the macro was called! But now if we change the quote definition (line 3) to



          quote location: :keep do


          We finally get the exact line where the error occurs:



          line 1
          line 2
          ** (ArithmeticError) bad argument in arithmetic expression
          test.exs:7: Test.my_function/0





          share|improve this answer


























          • The "location: :keep" was indeed the thing i was looking for! Thank you

            – Joachim Alvarez-Rodriguez
            Nov 25 '18 at 12:46



















          0














          What you are looking for is location: :keep option in the call to Kernel.quote/2.






          share|improve this answer
























          • Gabriel Prá also already answered with the "location: :keep", but the link you gave is also quite helpful!

            – Joachim Alvarez-Rodriguez
            Nov 25 '18 at 12:48











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460350%2fget-stacktrace-from-inside-code-eval-quoted%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          TLDR: Use location: :keep.





          You can define your quote inside a macro, like this:



          defmodule Test do
          defmacro my_quote do
          quote do
          IO.puts "line 1"
          IO.puts "line 2"
          5/0
          end
          end

          def try_quote do
          my_quote() # Line 11
          end
          end


          Now calling Test.try_quote:



          iex(7)> Test.try_quote
          line 1
          line 2
          ** (ArithmeticError) bad argument in arithmetic expression
          test.exs:11: Test.try_quote/0


          So we get line where the quote was called, which is better, but not yet what we wanted.



          A solution then could be to use the macro to define a function for us, like this:



          defmodule TestMacro do
          defmacro my_quote do
          quote do
          def my_function do
          IO.puts "line 1"
          IO.puts "line 2"
          5/0 # Line 7
          end
          end
          end
          end

          defmodule Test do
          import TestMacro
          my_quote # Line 15
          end


          Now calling Test.my_function gives us:



          line 1
          line 2
          ** (ArithmeticError) bad argument in arithmetic expression
          test.exs:15: Test.my_function/0


          Which is still the line where the macro was called! But now if we change the quote definition (line 3) to



          quote location: :keep do


          We finally get the exact line where the error occurs:



          line 1
          line 2
          ** (ArithmeticError) bad argument in arithmetic expression
          test.exs:7: Test.my_function/0





          share|improve this answer


























          • The "location: :keep" was indeed the thing i was looking for! Thank you

            – Joachim Alvarez-Rodriguez
            Nov 25 '18 at 12:46
















          0














          TLDR: Use location: :keep.





          You can define your quote inside a macro, like this:



          defmodule Test do
          defmacro my_quote do
          quote do
          IO.puts "line 1"
          IO.puts "line 2"
          5/0
          end
          end

          def try_quote do
          my_quote() # Line 11
          end
          end


          Now calling Test.try_quote:



          iex(7)> Test.try_quote
          line 1
          line 2
          ** (ArithmeticError) bad argument in arithmetic expression
          test.exs:11: Test.try_quote/0


          So we get line where the quote was called, which is better, but not yet what we wanted.



          A solution then could be to use the macro to define a function for us, like this:



          defmodule TestMacro do
          defmacro my_quote do
          quote do
          def my_function do
          IO.puts "line 1"
          IO.puts "line 2"
          5/0 # Line 7
          end
          end
          end
          end

          defmodule Test do
          import TestMacro
          my_quote # Line 15
          end


          Now calling Test.my_function gives us:



          line 1
          line 2
          ** (ArithmeticError) bad argument in arithmetic expression
          test.exs:15: Test.my_function/0


          Which is still the line where the macro was called! But now if we change the quote definition (line 3) to



          quote location: :keep do


          We finally get the exact line where the error occurs:



          line 1
          line 2
          ** (ArithmeticError) bad argument in arithmetic expression
          test.exs:7: Test.my_function/0





          share|improve this answer


























          • The "location: :keep" was indeed the thing i was looking for! Thank you

            – Joachim Alvarez-Rodriguez
            Nov 25 '18 at 12:46














          0












          0








          0







          TLDR: Use location: :keep.





          You can define your quote inside a macro, like this:



          defmodule Test do
          defmacro my_quote do
          quote do
          IO.puts "line 1"
          IO.puts "line 2"
          5/0
          end
          end

          def try_quote do
          my_quote() # Line 11
          end
          end


          Now calling Test.try_quote:



          iex(7)> Test.try_quote
          line 1
          line 2
          ** (ArithmeticError) bad argument in arithmetic expression
          test.exs:11: Test.try_quote/0


          So we get line where the quote was called, which is better, but not yet what we wanted.



          A solution then could be to use the macro to define a function for us, like this:



          defmodule TestMacro do
          defmacro my_quote do
          quote do
          def my_function do
          IO.puts "line 1"
          IO.puts "line 2"
          5/0 # Line 7
          end
          end
          end
          end

          defmodule Test do
          import TestMacro
          my_quote # Line 15
          end


          Now calling Test.my_function gives us:



          line 1
          line 2
          ** (ArithmeticError) bad argument in arithmetic expression
          test.exs:15: Test.my_function/0


          Which is still the line where the macro was called! But now if we change the quote definition (line 3) to



          quote location: :keep do


          We finally get the exact line where the error occurs:



          line 1
          line 2
          ** (ArithmeticError) bad argument in arithmetic expression
          test.exs:7: Test.my_function/0





          share|improve this answer















          TLDR: Use location: :keep.





          You can define your quote inside a macro, like this:



          defmodule Test do
          defmacro my_quote do
          quote do
          IO.puts "line 1"
          IO.puts "line 2"
          5/0
          end
          end

          def try_quote do
          my_quote() # Line 11
          end
          end


          Now calling Test.try_quote:



          iex(7)> Test.try_quote
          line 1
          line 2
          ** (ArithmeticError) bad argument in arithmetic expression
          test.exs:11: Test.try_quote/0


          So we get line where the quote was called, which is better, but not yet what we wanted.



          A solution then could be to use the macro to define a function for us, like this:



          defmodule TestMacro do
          defmacro my_quote do
          quote do
          def my_function do
          IO.puts "line 1"
          IO.puts "line 2"
          5/0 # Line 7
          end
          end
          end
          end

          defmodule Test do
          import TestMacro
          my_quote # Line 15
          end


          Now calling Test.my_function gives us:



          line 1
          line 2
          ** (ArithmeticError) bad argument in arithmetic expression
          test.exs:15: Test.my_function/0


          Which is still the line where the macro was called! But now if we change the quote definition (line 3) to



          quote location: :keep do


          We finally get the exact line where the error occurs:



          line 1
          line 2
          ** (ArithmeticError) bad argument in arithmetic expression
          test.exs:7: Test.my_function/0






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 5 '18 at 20:38

























          answered Nov 24 '18 at 18:17









          Gabriel PráGabriel Prá

          1,049616




          1,049616













          • The "location: :keep" was indeed the thing i was looking for! Thank you

            – Joachim Alvarez-Rodriguez
            Nov 25 '18 at 12:46



















          • The "location: :keep" was indeed the thing i was looking for! Thank you

            – Joachim Alvarez-Rodriguez
            Nov 25 '18 at 12:46

















          The "location: :keep" was indeed the thing i was looking for! Thank you

          – Joachim Alvarez-Rodriguez
          Nov 25 '18 at 12:46





          The "location: :keep" was indeed the thing i was looking for! Thank you

          – Joachim Alvarez-Rodriguez
          Nov 25 '18 at 12:46













          0














          What you are looking for is location: :keep option in the call to Kernel.quote/2.






          share|improve this answer
























          • Gabriel Prá also already answered with the "location: :keep", but the link you gave is also quite helpful!

            – Joachim Alvarez-Rodriguez
            Nov 25 '18 at 12:48
















          0














          What you are looking for is location: :keep option in the call to Kernel.quote/2.






          share|improve this answer
























          • Gabriel Prá also already answered with the "location: :keep", but the link you gave is also quite helpful!

            – Joachim Alvarez-Rodriguez
            Nov 25 '18 at 12:48














          0












          0








          0







          What you are looking for is location: :keep option in the call to Kernel.quote/2.






          share|improve this answer













          What you are looking for is location: :keep option in the call to Kernel.quote/2.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 7:43









          Aleksei MatiushkinAleksei Matiushkin

          82.9k95692




          82.9k95692













          • Gabriel Prá also already answered with the "location: :keep", but the link you gave is also quite helpful!

            – Joachim Alvarez-Rodriguez
            Nov 25 '18 at 12:48



















          • Gabriel Prá also already answered with the "location: :keep", but the link you gave is also quite helpful!

            – Joachim Alvarez-Rodriguez
            Nov 25 '18 at 12:48

















          Gabriel Prá also already answered with the "location: :keep", but the link you gave is also quite helpful!

          – Joachim Alvarez-Rodriguez
          Nov 25 '18 at 12:48





          Gabriel Prá also already answered with the "location: :keep", but the link you gave is also quite helpful!

          – Joachim Alvarez-Rodriguez
          Nov 25 '18 at 12:48


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460350%2fget-stacktrace-from-inside-code-eval-quoted%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Tonle Sap (See)

          I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

          Guatemaltekische Davis-Cup-Mannschaft