amnu3387

amnu3387

Getting Line Error on stack trace when "using" modules

I had a very large module (for my experience) with around 2k loc. I broke it down for organisational purposes, although I’m just “importing” these new modules into a main one. Now whenever I have an error that occurs in one of the modules being used I get the stack trace with the line where the module is imported, along with the function name and arity.

** (ArgumentError) could not put/update key nil on a nil value
    (elixir) lib/access.ex:371: Access.get_and_update/3
    (elixir) lib/kernel.ex:1880: Kernel.put_in/3
    (AetherWars) lib/aetherwars/duels/duels_processor.ex:12: AetherWars.Duels.Processor.decide_next_player/1
    (AetherWars) lib/aetherwars/duels/duels_processor.ex:11: AetherWars.Duels.Processor.pop_stack/4
    (AetherWars) lib/aetherwars/duels/duels_processor.ex:22: AetherWars.Duels.Processor.entry_point/4
    (AetherWars) web/channels/duel_channel.ex:25: AetherWars.DuelChannel.handle_in/3
    (phoenix) lib/phoenix/channel/server.ex:244: anonymous fn/4 in Phoenix.Channel.Server.handle_info/2
    (AetherWars) lib/aetherwars/endpoint.ex:1: AetherWars.Endpoint.instrument/4
    (stdlib) gen_server.erl:616: :gen_server.try_dispatch/4
    (stdlib) gen_server.erl:686: :gen_server.handle_msg/6
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3

Sometimes this is all I need, but other times I have several different functions with the same arity pattern-matching on the head. Is there any way to be able to see in which line of the original module that has been used it occurs? (I guess since use when compiled basically places the code on the module calling it, it won’t be straightforward but perhaps there’s some easy work-around?)

You can see it becomes a bit problematic:

defmodule AetherWars.Duels.Processor do
  
  use AetherWars.Duels.Create
  use AetherWars.Duels.Utils
  use AetherWars.Duels.Payment
  use AetherWars.Duels.Legal
  use AetherWars.Duels.Messages
  use AetherWars.Duels.Strike
  use AetherWars.Duels.Guard
  use AetherWars.Duels.Stack
  use AetherWars.Duels.Turn
  use AetherWars.Duels.Manipulation
  use AetherWars.Duels.Battle
  alias AetherWars.Duels.Monitor
  ...
end

Any solution? Thanks

Marked As Solved

ericmj

ericmj

Elixir Core Team

Generated code will use the stacktrace location of the caller. This is a good default for most macros since generated code should be kept to a minimum, the fact that you need the location of the generated code in the stacktrace is an indication that you are overusing it. Using use that expands to function definitions with the full implementation for composition is an anti-pattern.

If you do need the line information from the macros add location: :keep to the quoted code: quote location: :keep do ... end.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey! Large modules can definitely be a pain, but this is not the way to solve them. Find boundaries, break those things out into dedicated modules, then just have them each call each other.

amnu3387

amnu3387

@benwilson512 thanks for your input - and although I will probably end up doing that, I still think the error should be shown from its line on the used module and not the line where the use statement is (not sure how difficult it is to achieve in technical terms…).

amnu3387

amnu3387

Thanks @ericmj - that indeed answers my question. I will nonetheless try organising my code in a different way, following ben’s and your advice - in my head - probably wrong - those functions would belong together in the same module, perhaps except messages and create, since they are all operations on the game state (but I will probably curse the future me about that choice).

Last Post!

amnu3387

amnu3387

Besides my mental model when projecting this (and having to change function calls in so many places), nothing is wrong with calling functions where they’re located in the source code - I tend to do that usually ah. alias still implies calling the module name, and import I think I tried but then I would need to “import” things in multiple places - like in the sample, I would need to import “utils” & “legal” & “payment” and then in “payment” and “legal” probably import “utils” again, and so on, otherwise they would break.

Perhaps it’s just bad architecture :slight_smile: but given that I’m creating it as I go and don’t have the whole engine yet designed/thought about…

Thanks for your patience and suggestions. I will give this some thought (to break it up in the future) - and for now just add the trace

Where Next?

Popular in Questions Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement